You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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).
419
419
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`:
#[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
+
420
473
## Paginating Data
421
474
422
475
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
784
837
785
838
Read the detailed documentation about [Laravel data validation in API Platform](validation.md).
786
839
787
-
## Authorization
840
+
###Authorization
788
841
789
842
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):
790
843
@@ -796,10 +849,6 @@ Laravel will automatically detect your new policy and use it when manipulating a
796
849
797
850
Read the detailed documentation about using [Laravel gates and policies with API Platform](security.md).
0 commit comments