New In Laravel 7 - e06 - Route Model Binding Improvements
HTML-код
- Опубликовано: 3 июл 2024
- Route Model Binding is one of the coolest and most used features of Laravel. Laravel 7 comes with some welcomed changes which include the new key customization and automatic scoping in nested routes. Let's take a look at some concrete example of how it works.
Foreign Devs Podcast
foreign-devs.transistor.fm/su...
Documentation
laravel.com/docs/master/routi...
For the best experience, follow along in our interactive school at www.coderstape.com
DigitalOcean Referral
m.do.co/c/7dce5364ef4d
It's that time of the year again when we get new fresh features in Laravel. Let's explore what is coming to the latest version of Laravel.
please start a series on any famous ecommerce laravel package
thanks victor!!! this is really awesome feature!
Thanks for making this video
My most awaited feature :D
Dynamic view without a controller... Magic 💫😱😍
thx brother! love laravel!!!
you are one the best explainer. always loved your content. if you can please upload a video about larvel 6 to 7 upgrade guide
Thank you!
Thanks 👍
Good video Bro, you should do one that says 50 tricks with laravel
tanx 🙏 ❤
Your great man ❤
Hi Victor! Would you do any tutorial on how to build SPA with Laravel, Vue, and Inertia.js?
THANKS
I was actually thinking how to put a slug in my laravel 6 project and this makes me to finally update my laravel version :D
You don't need to upgrade to do that, he showed you the old method in the video.
@@lucassnel9064 oh, thanks buddy
Oh wow... This is definitely going to be helpful. Thank you!
One question: what if we have /user/1/post/2/comment/3 (all have in the route ":id") ?
It will be "comment" for that "port" for that "user"?
Is there a way to make it another level up like:
Get the "comment" for that "post" for thet "user" AND the "comment" has to be from that "user"
I love Laravel, I really love it and hate at the same time !!!
Thanks this is awesome. Who would you handle something like Catgory/subcategory/subcategory/...... I think this is also real when i can have lets say /category2/subcategory wit the same name as category1. However i cant figure out how to next more subcategories.
This is going to be really useful for what i'm working on now. Are you planning to release a video on how to do upgrades from v6 to v7?
I don't think it will be too complicated of an upgrade since there aren't any deprecations that I can think of
@@CodersTape I've only started looking at Laravel a week ago and didn't realise that a new version was coming out. I'd also need to look into how to update a project to a more recent version in the same main version.
What would the protection do if the column in the posts table was `author_id`?
Laravel will check models relatioship, wherever the field you need.
```
return $this->belongsTo(User::class, 'author_id');
```
so if I want to pull id with slug, not just id or slug, I must use {post:id}-{post:slug} ?
do the airlock please :D
How to customize model binding in resource controller?
How does the model binding work for resource controller with nested multiple parameters ?
Example:
localhost/users/1/posts/2/comments/3
( where 'posts' table has 'user_id' foreign key and 'comments' table has 'post_id' foreign key )
Using:
Route::resource('users.posts.comments', 'BlogController');
instead of doing:
Route::name('users.posts.comments.index')->get('users/{user}/posts/{post:id}/comments', 'BlogController@index');
Route::name('users.posts.comments.create')->get('users/{user}/posts/{post:id}/comments/create', 'BlogController@create');
Route::name('users.posts.comments.show')->get('users/{user}/posts/{post:id}/comments/{comment:id}', 'BlogController@show');
Route::name('users.posts.comments.edit')->get('users/{user}/posts/{post:id}/comments/{comment:id}/edit', 'BlogController@edit');
Route::name('users.posts.comments.update')->patch('users/{user}/posts/{post:id}/comments/{comment:id}/update', 'BlogController@update');
Route::name('users.posts.comments.store')->post('users/{user}/posts/{post:id}/comments', 'BlogController@store');
Route::name('users.posts.comments.destroy')->delete('users/{user}/posts/{post:id}/comments/{comment:id}', 'BlogController@destroy');
How to use specify key name in resource route???
Very strange., its doesn't work for me!
Route::get('/person/{name}','PersonSiteController@show');
And in dump don`t choose id, I see only dump of instance. Do any one know what a problem?
Is the protection thing a bug? Or is it because the ID is unique to each post, but slugs aren't necessarily? So for example, "/users/1/posts/a-cool-post" and "/users/2/posts/a-cool-post" are two completely different posts.
when you are using slugs as keys, you must have unique constraint on that column, otherwise, you are right. If slugs can be repeated than it can't be used as a substitute for a unique identifier.
Thanks for the video as always.
I'm not sure what we're gaining here, it looks like another way to do something we could already very easily do with getRouteKeyName().
What if we use the Route::resource method? Looks like it wouldn't work and would require to use the getRouteKeyName() method anyway...
Also not sure about the extra protection added to the route model binding; I feel like this authorization logic belongs in a Policy. That could be confusing to debug IMHO.
The benefit is that for example the user-end you want to bind posts based on slug, but in the admin panel you might want to bind on ID. So this way it's on a per-route basis instead of globally set once in the model.
@@lucassnel9064 I didn't think about that. Thank you for the explanation 👍
what if my slug is not in the database, iam getting not found since i dont have slug field and im using accessor, solution please?
Why is the default with mandatory id ? I mean, the binding concept is that i don't have to tell to Laravel look at the post id
Because the default of $primaryKey on base model is 'id', You can override it on your model.
I think that that automatic scoping has the potential to break some code in existing projects so adding that "opt in" feature will ensure it is 100% backwards compatible. Imagine that you didn't want that scoping, then there would be no way to "opt out"
@@CodersTape Ok, it's just compatibility stuff so. Thank you
Please insert nex video 🙏❤
So now Laravel 7 is optimized for blog development (I assumed)?
But yeah, hidden product listing details owned by a seller could be useful too.
wokeh?
That caveat is too big to ignore yet too small to overlook