Laravel API Resource to Re-Use in Other API Resources

preview_player
Показать описание
I want to demonstrate a "trick" that you can create a separate API Resource and reuse it in other Resources.

- - - - -
Support the channel by checking out my products:

- - - - -
Other places to follow:
Рекомендации по теме
Комментарии
Автор

i have been using this re-use of resource 4 years ago on one of our projects, pretty helpful

gamesandlofimucic
Автор

I did this exact thing some 6 months ago with an api I was asked to refactor. I has a lot of structures that use same data in same way, so I assigned an api resource for each of them, then interconnected them.

marcusgaius
Автор

API Resources within another API Resource is extremely useful and one of the bigger reasons to use these resources in the first place.

For example if you want to return an Author resource and also return a list of Books they have associated with their record, you just attach books to the author resource and call the appropriate method to give back an array of those Book resources using the list of books as a source.

JouvaMoufette
Автор

pretty useful for "standard" objects, like User for instance!

giacomogaravaglia
Автор

I have learned alot from Alex he is great.

khizer
Автор

This is really nice one.

It is usually tiring formating in every resource

codewithtee
Автор

And again DI is murdered and another bloated package is loaded for something simple.

bremg
Автор

another useful way is you can set it in your model like:

public function getCreatedTimeAttribute()
{
return
}
protected $appends = ['created_time'];
protected $casts = [
'created_at' => 'datetime:F d Y, h:i A',
];

jamaalcham