Junior Code Review: Simple Laravel API - in 5 Different Ways

  Рет қаралды 76,079

Laravel Daily

Laravel Daily

Күн бұрын

I've given a task to my students to create two simple API endpoints in Laravel. Let's see how they did and what advice I can give to them.
- - - - -
Support the channel by checking out our products:
- Try our Laravel QuickAdminPanel: bit.ly/quickadminpanel
- Enroll in my Laravel courses: laraveldaily.teachable.com
- Purchase my Livewire Kit: livewirekit.com
- Subscribe to my weekly newsletter: bit.ly/laravel-newsletter

Пікірлер: 80
@Uli2000Ger
@Uli2000Ger 3 жыл бұрын
If you are shown and evaluated different ways of solving the problem, you can learn even more. Really a good idea.
@JonathanKingstonFear
@JonathanKingstonFear 3 жыл бұрын
From my recent experience when writing the list you only want to explicitly load the columns you need to save some memory usage as it can quickly add up with large lists, especially if you aren't using server-side pagination. You can also save additional memory usage and database traffic by using LEFT JOIN authors and SELECT GROUP_CONCAT on authors' name if all you need is a comma separated list of their names, something like this: Books::select('id', 'name', 'price', 'cover', DB::raw('GROUP_CONCAT(CONCAT_WS(" ", authors.first_name, authors.last_name)) SEPARATOR ", " AS author_name') ->get()
@devsnoob
@devsnoob 7 ай бұрын
❤❤
@poplach
@poplach 3 жыл бұрын
8:43 whenAppended is just a condition, whether to return a VALUE of description or not. Description will be returned for book list as well but will always be empty, which is very confusing and wrong. I'd add that the only correct way in my opinion is with merging arrays, since field 'description' will not be returned if you access a list of books.
@HussamAdil
@HussamAdil 3 жыл бұрын
great video, Changing RouteServiceProvider just to add v1 can break other API code because it will change all routes in api file to use v1
@DragonCzz
@DragonCzz 3 жыл бұрын
It probably won't, maybe on the integration side where 3rd party consumers have to do the exact same change. The idea is to use named routes in every single scenario when generating routes, which would make actual routes independent from their path.
@JeppeBeier
@JeppeBeier Жыл бұрын
I see adding the v1 in the provider as a valid solution, but you avoid deprecation in the future you would then just need to have an individual api file for each of the versions. You could simply name the files accordingly.
@NathanBudd
@NathanBudd 3 жыл бұрын
This is great! Really useful. Perhaps a video before showing the task, so people can try and follow along as well? Then an example of how you'd do it?
@josuebarros-desenvolvedorw2490
@josuebarros-desenvolvedorw2490 2 жыл бұрын
Incredible content, thank you! Its hard to find API content on brazilian courses
@nazeerkaadan
@nazeerkaadan 3 жыл бұрын
Great teacher && Great explanation... Useful as always Many thanks.
@olenstriker
@olenstriker 3 жыл бұрын
I don't even know php and Lavarel, but these reviews and notes are really usefull! I'm interessed in starting learning the language based on your review videos!
@ivandez1811
@ivandez1811 3 жыл бұрын
Go ahead
@yudistiraanaga
@yudistiraanaga 3 жыл бұрын
best youtube channel for laravel tips
@blessdarah1256
@blessdarah1256 2 жыл бұрын
The way you teach is very spectacular.
@100dollerbill4
@100dollerbill4 3 жыл бұрын
apiresource this is something new which i have learn today thank you. will apply it in my next project
@phil86_
@phil86_ Жыл бұрын
Great video, very didactic the comparing of different possible solutions to the same problem
@hatsushiroyuki8697
@hatsushiroyuki8697 3 жыл бұрын
Nice video as usual. Thank you for sharing your knowledge!
@josetovarrodriguez3525
@josetovarrodriguez3525 3 жыл бұрын
it's awesome code, thanks for your efforts, keep doing that
@shire7949
@shire7949 3 жыл бұрын
Thank u very nice examples one can leqrn from each something
@zuhrilfahrizal7555
@zuhrilfahrizal7555 Жыл бұрын
Thanks for tutorials 🙏
@yangdu7194
@yangdu7194 3 жыл бұрын
You‘re a rock star in tech world
@tzalejo82
@tzalejo82 3 жыл бұрын
Gracias, genial el video!!
@martinfeldman7340
@martinfeldman7340 2 жыл бұрын
This is a great video!
@Salarieentrepreneur
@Salarieentrepreneur 3 жыл бұрын
Great video, thank's
@facundocondal4545
@facundocondal4545 2 жыл бұрын
First of all, I want to thank you for all the content that you share with the community. I have a question about the best way to version an API. How is the best structure to maintain different versions of an API. If you separate the Controllers and Resources into different folders, yo will have repetead code.
@LaravelDaily
@LaravelDaily 2 жыл бұрын
Well, you reuse some part of the code, but it delends on the exact situation, can't explain it in a KZfaq comment and without specific example.
@m.batbaatar7647
@m.batbaatar7647 3 жыл бұрын
Really helpful thanks
@johnyonardpauly5601
@johnyonardpauly5601 3 жыл бұрын
I normally do this: Books::select([specific columns only])->with('authors:id,name')->get(); or better use query builder with LEFT JOIN (faster than eloquent).
@khangle6872
@khangle6872 Жыл бұрын
When it comes to performance, building query is always faster than an ORM, but it also require more SQL knowledge and take more time to made. So depend on the use case, it is fine to just use eloquent
@RodrigoSantos-qg6xk
@RodrigoSantos-qg6xk 3 жыл бұрын
great tips!! :D
@mianz167
@mianz167 3 жыл бұрын
Great informative video as always. Please do a video on lazy loading of images. It will be really helpful.
@LaravelDaily
@LaravelDaily 3 жыл бұрын
I think lazy loading of images is a front-end topic, I'm trying to focus on Laravel or back-end stuff more.
@mianz167
@mianz167 3 жыл бұрын
Ok got it. Thanks for what you are really doing. Your videos keeps the beginner developers like me up to date. Keep up the good work 👍
@tuscala
@tuscala 3 жыл бұрын
Thank you!
@RodrigoNishino
@RodrigoNishino Жыл бұрын
Very nice thank you
@pkbissyer7751
@pkbissyer7751 3 жыл бұрын
Sir,Make a video on shipping api used in laravel any project such as ecommerce etc.
@TsA1ex
@TsA1ex 3 жыл бұрын
6:27 What if to add 'with' or even 'whenLoaded' inside BookResourceCollection instead of BookController
@AlexG-wk3nh
@AlexG-wk3nh 2 жыл бұрын
If i was to have a user dashboard which is only accessible through a login, and i want the user to be able to display all their customers, add, edit and delete customers, would i put all these in the web route? or just put the 'display all' customers in the Api route and the rest in web?
@Akosiyawin
@Akosiyawin 3 жыл бұрын
Hello, I tried using the API routes before (api.php), it works fine in my local development, But when I tried hosting it to cpanel, I got the Unauthorized or Unauthenticated error, I don't remember what it was exactly. All routes with middlware auth, cannot seem to be access. All routes response are Unauthenticated if I recall. Can't find any solution for that, so I moved all my API routes to Web routes instead and added a prefix of 'api'
@codewithtee
@codewithtee 3 жыл бұрын
Thanks, for this tip
@_passby5399
@_passby5399 3 жыл бұрын
Good video
@SXsoft99
@SXsoft99 2 жыл бұрын
what i haven't noticed any of your students do is select only what fields are needed for listing if you dont need a certain field dont use select(*), since a description db column might have 3k characters, and you only needed that field on your details page
@hafizshaheed6973
@hafizshaheed6973 2 жыл бұрын
How can I use web's guard and api's guard both in laravel project?
@shire7949
@shire7949 3 жыл бұрын
I didn't know about the apiResource
@iedi3
@iedi3 3 жыл бұрын
Hi guys, i keep seeing the statement “new Resource($book)”. Is there a way to avoid the “new” statement and use Dependency Injection in some way?
@kievthedeaf6756
@kievthedeaf6756 3 жыл бұрын
Does this help you out laravel.com/docs/8.x/container ?
@andywong2244
@andywong2244 Жыл бұрын
i believe you can use Resource::make($book)?
@salimmammadov7791
@salimmammadov7791 3 жыл бұрын
Awesome ! Can you make a video about how to make filter searching by the shortest way? Example : if ($request->cid != null) { $equ->where('category_id', $request->cid); } if ($request->mid != null) { $equ->where('model_id', $request->mid); } $result = $equ->paginate(15); return response()->json($result); }
@LaravelDaily
@LaravelDaily 3 жыл бұрын
Just search for Eloquent when() method: laraveldaily.com/less-know-way-conditional-queries/
@salimmammadov7791
@salimmammadov7791 3 жыл бұрын
@@LaravelDaily Great ! I am waiting for your next videos !
@vinitkumar9687
@vinitkumar9687 3 жыл бұрын
Hi poli, How to make a Website and API REST with the same Controllers? It is possible I'm totally confused, and how to create both are same Controllers, plz help
@alexanderanastasiadis9284
@alexanderanastasiadis9284 3 жыл бұрын
Is correct to create api through WEB instead API route file? And what are the differences
@undentifiedkc
@undentifiedkc 3 жыл бұрын
Web routes is "web middlewares and have checking for CSRF and session, while api routes is using "api" middleware which doesn't have CSRF and session i think there's more different, but this 2 are the biggest different
@andywong2244
@andywong2244 Жыл бұрын
it keeps your codes for the WEB and API separate and organized. separation of concerns.
@johnbiggs2945
@johnbiggs2945 3 жыл бұрын
Hi, Where did your students take this course? How can I register for this? Thanks
@LaravelDaily
@LaravelDaily 3 жыл бұрын
Hi John, for now it's closed for my local community in Lithuania. I may expand it internationally in a new cohort in Spring, still thinking about it. Follow the channel for any news.
@johnbiggs2945
@johnbiggs2945 3 жыл бұрын
​@@LaravelDailyHope you do expand! I will be one to register for! Does any of your courses at laraveldaily.teachable.com teach what was shown in the lesson? thanks
@mexvision-3556
@mexvision-3556 2 жыл бұрын
5:12 IMPLOOOOOOOOOOOOOOOOODEEEEEEE =))))
@j.oliveira
@j.oliveira 3 жыл бұрын
Route::resource for me is a no go. You are using a resource to then use only two methods. That only makes things harder IMO. You lose control of the naming and you don't see the whole picture when scanning the route file.
@anticom1337
@anticom1337 3 жыл бұрын
No one got the /api/v1 properly right IMHO. What I would do is close to your favorite implementation but instead of modifying the global prefix for the api routes from api to api/v1 I'd still introduce a route group wrapping the Route::apiResource(...). The reasoning is simple: What are you going to do when you have to implement api v2? Create a custom apiv2.php route file and load it in your Route-SP? I hope you see where I'm going with this.
@xUnholyPrayerx
@xUnholyPrayerx 3 жыл бұрын
To create v2 routes file is in my opinion a nice solution, you will never be in doubt what version you add or bugfixed logic
@LaravelDaily
@LaravelDaily 3 жыл бұрын
Yes I've done exactly that in multiple projects: routes/api_v2.php I don't see anything wrong with it.
@anticom1337
@anticom1337 3 жыл бұрын
@@LaravelDaily Upon thinking about it a little more I came to the conclusion that indeed in the end it's just a matter of preference. I thought it might maybe have some disatvantages when it comes to managing middleware groups and stuff like that but it doesn't. Thanks for the reply and showing some alternative to what I thought to be the only valid solution. ---------------------------------------------------------------------- For other interested readers of this, here are some thoughts about the differences between the two approaces: Having all your API routes in a single api.php file this means that over time your file will grow more and more as your application is growing. This in turn means that you have to have some discipline in keeping your route definitions organizes. In turn you get everything in one file which makes it easy to glance over it as well as finding specific info you might be looking for. On the other hand, having dedicated route files for the different API versions this keeps the files self-contained and well scoped for that single version of your API. This allows you to be a little more sloppy (which of course you shouldn't anyway!) with the structure of your route dfinitions. On the downside you loose the big picture without having multiple files open side by side.
@kelvezu1585
@kelvezu1585 3 жыл бұрын
Some of the methods discussed here was not in the documentation. As a beginner in Laravel, should I buy premium courses just to unlock these secret methods?
@LaravelDaily
@LaravelDaily 3 жыл бұрын
Which ones were not in the documentation? Well, documentation is open-source, so anyone could contribute to the documentation and add those methods there.
@patricksantino4131
@patricksantino4131 2 жыл бұрын
obviously there are some stuff that is not documented or already documented in previous laravel docs.
@abidaadhil9777
@abidaadhil9777 2 жыл бұрын
Ad 25..serch..?
@dekathomas
@dekathomas 2 жыл бұрын
Hello, could I join the junior developer community program?
@LaravelDaily
@LaravelDaily 2 жыл бұрын
It was a one-time local thing, already over.
@AbderrahmanFodili
@AbderrahmanFodili Жыл бұрын
You come across as a harsh tutor 😅 but this review made me feel better about my skills
@mhamad22690
@mhamad22690 2 жыл бұрын
Hello, can I send you a code I wrote and give me your opinion on it Thank you ❤️❤️
@LaravelDaily
@LaravelDaily 2 жыл бұрын
I don't do personal code reviews anymore, sorry, focused on other topics.
@mhamad22690
@mhamad22690 2 жыл бұрын
@@LaravelDaily It's okay Thank you ❤️
@leonardoldr
@leonardoldr 3 жыл бұрын
Wouldt be a better practice to keep the resource the dummiest as possible, and condition him with something like $this->when(Arr::exists($this, 'description'), $this->description ?? '') and then you just pick the data you want in the list and in the view? Seems like a better approach to give the resource the logic to only work with we give to him (and it be his only concern, even ignoring the existance of the request), and control our business logic in a better place like controller.
@patricksantino4131
@patricksantino4131 2 жыл бұрын
also a good approach. Thanks for the info
@supram941g5
@supram941g5 3 жыл бұрын
web.php or api.php it is not about route path (or right naming), it is about middlewares for request , obviously you didn't explain it to your students, and don't explain it in video. sad.
@LaravelDaily
@LaravelDaily 3 жыл бұрын
Good catch, I didn't emphasize that indeed.
Code Review. Laravel Versions by Tighten: 10+ Things to Learn
9:44
Laravel Daily
Рет қаралды 10 М.
9 Tips for Shorter Laravel Code
10:16
Laravel Daily
Рет қаралды 61 М.
ИРИНА КАЙРАТОВНА - АЙДАХАР (БЕКА) [MV]
02:51
ГОСТ ENTERTAINMENT
Рет қаралды 7 МЛН
Why You NEED To Learn FastAPI | Hands On Project
21:15
Travis Media
Рет қаралды 150 М.
Laravel SPA Authentication - setup and common mistakes
16:55
Exceptions in Laravel: Why/How to Use and Create Your Own
12:18
Laravel Daily
Рет қаралды 86 М.
Code Review with a Jr JavaScript Developer - Here's What I Found!
10:03
14 Quick Laravel Tips in 9 Minutes: May 2024
9:09
Laravel Daily
Рет қаралды 9 М.
Junior Code Review: Cleaning Up Laravel CRUD
14:54
Laravel Daily
Рет қаралды 68 М.
Laravel Controller Code: Move to Model, Service, Action or Job?
12:51
Laravel Daily
Рет қаралды 103 М.
Junior Developer Sent Me A PR For Review
17:26
Amigoscode
Рет қаралды 165 М.
Laravel Service Providers: All You Need to Know
13:13
Laravel Daily
Рет қаралды 63 М.
Think of stray animals 🙏😥
0:37
Ben Meryem
Рет қаралды 59 МЛН
How did we do? 👀😬😅 @RaenaTripleCharm 🍍 | Gabriella Triple Charm #shorts
0:19
Chúa ơi - Hãy thử cái này #automobile #funny #shorts
0:12
hoang quach
Рет қаралды 10 МЛН
VOCÊ ME AJUDARIA ?
0:46
JHONIC
Рет қаралды 17 МЛН