Laravel API Resource to Re-Use in Other API Resources

Laravel API Resource to Re-Use in Other API Resources

Laravel Daily

1 год назад

7,087 Просмотров

Ссылки и html тэги не поддерживаются


Комментарии:

Pramesh Dhakal
Pramesh Dhakal - 28.01.2023 18:12

I planned to do like this before 😉

Ответить
jamaalcham
jamaalcham - 21.01.2023 08:46

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

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

Ответить
Hans Herrera
Hans Herrera - 20.01.2023 19:05

I like this one a lot

Ответить
Jouva Moufette
Jouva Moufette - 20.01.2023 19:00

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.

Ответить
Victor Alicaya
Victor Alicaya - 20.01.2023 13:15

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

Ответить
Nelson Mutane
Nelson Mutane - 20.01.2023 11:51

Thanks for the tip.

Ответить
Giacomo Garavaglia
Giacomo Garavaglia - 20.01.2023 11:16

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

Ответить
Code_755
Code_755 - 20.01.2023 11:00

Thanks a lot for sharing.

Ответить
code with tee
code with tee - 20.01.2023 10:20

This is really nice one.

It is usually tiring formating in every resource

Ответить
MarcusGaius
MarcusGaius - 20.01.2023 10:07

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.

Ответить
Bremg
Bremg - 20.01.2023 09:54

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

Ответить
Akhil Jayaraj
Akhil Jayaraj - 20.01.2023 09:19

interesting

Ответить
Khizer
Khizer - 20.01.2023 09:13

I have learned alot from Alex he is great.

Ответить