<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
larsmbergvall / json-api-resources-for-laravel example snippets
public function books()
{
$books = Book::with(['author.country'])->take(2)->get();
return JsonApiResourceCollection::make($books)->withIncluded();
}
// ./app/Http/Kernel.php
protected $middlewareGroups = [
// ...
// Web middleware and such
// ...
'api' => [
// Other api middlewares
JsonApiMiddleware::class,
],
]
use Larsmbergvall\JsonApiResourcesForLaravel\Attributes\JsonApiIncludeAttributes;
use Larsmbergvall\JsonApiResourcesForLaravel\Attributes\JsonApiIncludeRelationships;
use Larsmbergvall\JsonApiResourcesForLaravel\Attributes\JsonApiType;
#[JsonApiType('books')] // Change `type` property
#[JsonApiIncludeAttributes(['title', 'isbn', 'year'])] // Which of the models attributes to
use Larsmbergvall\JsonApiResourcesForLaravel\JsonApi\JsonApiResource;
public function show(Book $book): JsonApiResource
{
return JsonApiResource::make($book);
}
use Larsmbergvall\JsonApiResourcesForLaravel\JsonApi\JsonApiResourceCollection;
public function index(): JsonApiResource
{
// Can also be used with pagination:
// return JsonApiResourceCollection::make(Book::orderBy('title')->paginate());
return JsonApiResourceCollection::make(Book::all());
}