PHP code example of focus / json-api

1. Go to this page and download the library: Download focus/json-api library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

focus / json-api example snippets


use Focus\JsonApi\DocumentData;

$data = DocumentData::fromRequest($request);

$book = $data->resource();

var_dump($book->attribute(path: 'title'));
var_dump($book->attribute(path: 'subtitle'));
var_dump($book->relation(name: 'author'));

$books = $data->collection();

var_dump($books->ids());

$publishers = $data->s->ids());

$id = $data->id();
$type = $data->type();

$id = $data->id();
$topics = $data->attribute(path: 'topics');
$author = $data->relation(name: 'author');
$publishers = $data->relations(name: 'publishers');

var_dump($publishers->ids());

foreach ($publishers as $publisher) {
    var_dump($publisher->id());
    var_dump($publisher->type());
}

var_dump($publishers->ids());

foreach ($publishers as $publisher) {
    var_dump($publisher->id());
    var_dump($publisher->attribute(path: 'name'));
}