Laravel API Resource - Reusable with Conditions For Fields

preview_player
Показать описание
A follow-up to a previous video on creating different API Resources for different endpoints: what if we decide to re-use the same resource class, after all?

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

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

Nice! What I usually do, is in the controller add some param in the request, so then in the resource I know what to do, and create the return array from there. I think that it should be interesting if Laravel allows to send more data in the resource, it's very limited

marcgarcia
Автор

I like the videos about API resources. Now that laravel goes more into SPA (at least that's how I see it) Livewire, replacement of webpack with vite, APIs becoming more interesting and important. But that's just my prediction where Laravel wants to go.

noplay
Автор

Thank you so much for the tips, this really help me in my current project, before I used to created multiple api resources, but now I can replace with only one which save a lot of time and work.
Thank you I really appreciate. watching your videos everyday from Nigeria 👍

techjeed
Автор

You can do also ternary operator in the key of array or if else conditions to add a new element in array

kirayamato
Автор

Thank you for this. First time I'm seeing the $this->when in the resource

bballantyne
Автор

To separate a list of objects from single object i use ResourceCollection instead of JsonResource.

benedettosciuto
Автор

Sir, is there any similar options with FormRequests validations? For example we have an Alarm API, where all users table has timezone column with some timezone value (UTC+3). In the resource of alarm(s) we transform all data to the user’s timezone. But when the user sends post request to API to create an alarm at 2022-07-20 09:00:00, in FormRequest we have to:
1) validate time value is filled (required)
2) validate time value has correct date time format
3) if everything is ok - transform all data to UTC and go ahead to the Alarms controller.

ZhanOmar
Автор

I tried this way to conditional rendering for relationships, and whenLoaded method doesnt work as expected and I had to use different resources

rmzomgo
Автор

I think in this case we could have used match experssion.

ricards
Автор

what is the purpose of putting the routeIs condition in the isWhen as the logic is dont in the callback? could you just put nothing in here?

topviralclips_x
Автор

Thanks for the video. I learnt a lot from your videos. Can you please tell me where to store large static data in laravel so that we can use in multiple places. I am storing them directly in blade template as array inside @php . But Its very repitative. (Please make a video on this topic if possible)

rickythegermanshepherd
Автор

Thanks for your video. I have a question: when you used "$this->when" I expected the value of description to be empty, instead even the key disappear in Postman. Can you explain me why? What am I missing? Thanks.

IlPandax
Автор

Is it okay to send message and status from API controller?

alishan
Автор

I know this is not related to today's video but allow me to drop it here. I'm building an e-commerce system and would like to integrate a product recommendation engine. Apart from creating the model in python and interacting with it using APIs, is there a "Laravel-ish" way of doing this?

denniswanjiru
Автор

In description you don't paste link to your newsletter.

cubestorm
Автор

can you make all of your course available on udemy?

sidecharacter
Автор

thats a good approach but you're kinda turning it into a graphql api LOL

ricko