Laravel Return Types in API Controller: Five Options?

  Рет қаралды 4,800

Laravel Daily

Laravel Daily

Күн бұрын

I was wondering what should API Controller methods return. Let me show you what I found in this experiment.
Links mentioned in the video:
My tweet with the same question: x.com/PovilasK...
My video "Parameter Types in Laravel 10: Why It's So Important" • Parameter Types in Lar...
Eloquent API Resources docs: laravel.com/do...
Support the channel by checking out my products:
My Laravel courses: laraveldaily.c...
Filament examples: filamentexampl...
Livewire Kit Components: livewirekit.com
Other places to follow:
My weekly Laravel newsletter: us11.campaign-...
My personal Twitter: / povilaskorop

Пікірлер: 18
@gtsmeg3474
@gtsmeg3474 Ай бұрын
I consistently return either a JsonResponse or a Response (for a 204 status) as the function’s return type. It’s always a good practice to enforce typing everywhere, including on function arguments.
@nuhuibrahim1772
@nuhuibrahim1772 28 күн бұрын
I actually like the JsonResource type in this case. Thank you for sharing. I learn something new every day from your post.
@ricko13
@ricko13 Ай бұрын
good to see you back uncle Povs, happy new year ✨
@JJ15ASMR
@JJ15ASMR Ай бұрын
Welcome back, I hope you and your family had a good holiday and new year. I'm definitely one for return types in controllers. I tend to collapse methods in my editor when not actively working on or reviewing them in depth, so the return type also adds some visual information and just feels "right". But I can also understand why they could be seen as redundant, as it's pretty standard for the `index()` or `show()` method to always return a view, `store()` to always return a redirect, etc. But as you say, it's a personal preference.
@donmikele07
@donmikele07 Ай бұрын
It's always good to have return types from methods and that is the best practice from strong typed languages that PHP (Laravel) should follow in my humble opinion. It's helpful for many reasons.
@Илья-щ3ф3и
@Илья-щ3ф3и Ай бұрын
We wrap resource (or collection) into response()->json() and always return JsonResponse from controller.
@object_name
@object_name Ай бұрын
From my point of view there are a few mistakes with the final takeaway. First of all "mixed" is not useless, but indicates "Hey we cannot specify this right now, but we have thought about it" as opposed to having forgotten a return type completely. Furthermore, if anything the Framework should define the return type of all controllers like "{ [key :string] : (...args: any[]) => Responsable }" (which i do not think is possible right now in php), but like that it is easier for me as a developer to know what i am allowed to return with laravel being able to handle it correctly. Therefor i would have preferred the stubs to use Responsable, i think. And finally, i think this is where Laravel breaks down or rather its approach to do so much "Magic" and "under the hood conversion". It makes it harder to really define what is expected as a result. In most cases i dislike magic methods, besides the "fancy" factor. If you want to have good type hints they become obsolete as instead of writing the function, you have to annotate the property/method instead. My suggestion would be to have more strict expectations in return types. But have some methods that allow various many to one conversions like for example "makeResponsable", which takes anything a controller can return currently and returns a strict type, ready to be send back.
@Tyche.Crypto
@Tyche.Crypto Ай бұрын
Thanks for sharing, I was searching for this.
@mustafa0x02
@mustafa0x02 Ай бұрын
Thanks 🤜🤛
@brianmweudeveloper
@brianmweudeveloper Ай бұрын
The preference is the best way.
@WatsonCyrusAnikwai
@WatsonCyrusAnikwai Ай бұрын
Thanks.
@thaerzuirkat4345
@thaerzuirkat4345 Ай бұрын
Welcome back.
@AbderrahmanFodili
@AbderrahmanFodili 28 күн бұрын
I usually do ``` return response()->json([ 'tasks'=>TaskResource::collection($tasks) ]); ``` it does feel repetetive . what do you think?
@morteza55510
@morteza55510 Ай бұрын
I do return a resource or resource collection but like so return response()->json(['data' => ModelResource::make($model), ...]); So i get the JsonResponse return type
@BsiennKhan
@BsiennKhan 9 күн бұрын
I use static analysis tools and they require return types on everything, so my controllers look like this: public function index(Request $request): JsonResponse|View { if ($request->expectsJson()) { $data = $this->service->paginate(request()->integer('perPage', 10)); return response()->json($data); } return view('index'); }
@n1ebieski12
@n1ebieski12 Ай бұрын
->response() returns JsonResponse
@trewlove
@trewlove Ай бұрын
Always returning JsonResponse or JsonResource
Laravel Mini CRM for Beginners in 1 Hour: Step-by-Step
1:12:57
Laravel Daily
Рет қаралды 17 М.
Chain Game Strong ⛓️
00:21
Anwar Jibawi
Рет қаралды 41 МЛН
小丑女COCO的审判。#天使 #小丑 #超人不会飞
00:53
超人不会飞
Рет қаралды 16 МЛН
BIG News From Laracon EU 2025: My Opinions
14:15
Laravel Daily
Рет қаралды 6 М.
This New Speculation API Will Make Your Site 10x Faster
20:55
Web Dev Simplified
Рет қаралды 86 М.
PHPStan is TypeScript for PHP!
7:15
Nuno Maduro
Рет қаралды 3,9 М.
4 Problems with Eloquent Soft Deletes (and Two Alternatives)
9:05
Laravel Daily
Рет қаралды 16 М.
Coding Was HARD Until I Learned These 5 Things...
8:34
Elsa Scola
Рет қаралды 909 М.
Laravel Gems - Pipelines 💎
15:21
Laravel
Рет қаралды 13 М.
What Senior Devs ACTUALLY Do? (And how to become one)
7:12
Laravel Daily
Рет қаралды 9 М.
7 Design Patterns EVERY Developer Should Know
23:09
ForrestKnight
Рет қаралды 317 М.