Skip to content

Commit 846e383

Browse files
authored
laravel groups (#2163)
1 parent da9f049 commit 846e383

File tree

1 file changed

+54
-5
lines changed

1 file changed

+54
-5
lines changed

laravel/index.md

Lines changed: 54 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,59 @@ Content-Type: application/merge-patch+json
417417

418418
There's a powerful mechanism inside API Platform to create routes using relation (e.g.: `/api/authors/2/books`), read more about [subresources here](../core/subresources.md).
419419

420+
If you need to embed data, you can use [serialization groups](/core/serialization.md). Note that when you apply groups on Eloquent models they don't have properties therefore you need to specify groups using `#[ApiProperty(property: 'title')]`. Here's an example to embed the `author`:
421+
422+
```php
423+
namespace App\Models;
424+
425+
use ApiPlatform\Metadata\ApiResource;
426+
use Illuminate\Database\Eloquent\Model;
427+
428+
#[ApiResource(normalizationContext: ['groups' => ['book:read']])]
429+
#[ApiProperty(serialize: new Groups(['book:read']), property: 'title')]
430+
#[ApiProperty(serialize: new Groups(['book:read']), property: 'description')]
431+
#[ApiProperty(serialize: new Groups(['book:read']), property: 'author')]
432+
class Book extends Model
433+
{
434+
public function author(): BelongsTo
435+
{
436+
return $this->belongsTo(Author::class);
437+
}
438+
}
439+
```
440+
441+
If you need a group on every properties use the `Group` attribute on the class (note that we use the same group as specified on the Book's normalizationContext):
442+
443+
```php
444+
namespace App\Models;
445+
446+
use ApiPlatform\Metadata\ApiResource;
447+
use Illuminate\Database\Eloquent\Model;
448+
449+
#[ApiResource)]
450+
#[Groups(['book:read'])]
451+
class Author extends Model
452+
{
453+
}
454+
```
455+
456+
You'll see:
457+
458+
```json
459+
{
460+
"@context": "/api/contexts/Book",
461+
"@id": "/api/books/1",
462+
"@type": "Book",
463+
"name": "Miss Nikki Senger V",
464+
"isbn": "9784291624633",
465+
"publicationDate": "1971-09-04",
466+
"author": {
467+
"@id": "/api/author/1",
468+
"name": "Homer"
469+
}
470+
}
471+
```
472+
420473
## Paginating Data
421474

422475
A must have feature for APIs is pagination. Without pagination, collection responses quickly become huge and slow,
@@ -784,7 +837,7 @@ You can create your own `Error` resource following [this guide](https://api-plat
784837

785838
Read the detailed documentation about [Laravel data validation in API Platform](validation.md).
786839

787-
## Authorization
840+
### Authorization
788841

789842
To protect an operation and ensure that only authorized users can access it, start by creating a Laravel [policy](https://laravel.com/docs/authorization#creating-policies):
790843

@@ -796,10 +849,6 @@ Laravel will automatically detect your new policy and use it when manipulating a
796849

797850
Read the detailed documentation about using [Laravel gates and policies with API Platform](security.md).
798851

799-
<!-- ## Testing the API
800-
801-
TODO-->
802-
803852
## Using the JavaScript Tools
804853

805854
### The Admin

0 commit comments

Comments
 (0)