Perfect content. This is almost assumed knowledge, but it's not something I ever came across when first delving into Laravel. Consequently I learned by osmosis which isn't ideal. I know you'll solve this problem for others with your new learning roadmap. Great stuff.
Is there any solutions for route model binding with hasMany relationships? If a user has many products, auth()->user()->products()->findOrFail($product) works fine, but with route model binding, user can get access to other products. Middleware checking will create an extra load (double DB request).
Today I am extremely happy... IT have upgraded our LTS server to the latest SP which includes PHP 7.4, so I can finally start playing around with Laravel 8! Oh, and nice vid Povilas as always!
is the capital Email and Password affect in attempt method in laravel i add field name in camalcase in database. if(Auth:: attempt (['Email'=>$request->email,'Password'=>$request->password])) this will return always false if i used Email and Password instead of email and password. can you tell me why this happening??
One more thing to add. If you want to bind a resource, nested resource or any route, you can you use scope as well. You can use it when you have multiple route model binding and you don't need to use the getRouteKeyName function
Sir awesome knowledge which you are sharing with us. Thanks for this. Sir i have question that what is alternative of id in url. And is it safe to show id in url.
You can probably do something like this: Route::get('/resource/{id_or_slug}', function ($id_or_slug) { $isId = ctype_digit($id_or_slug); if ($isId) { // find as id return Resource::findOrFail($id_or_slug); } else // find as slug { return Resource::where('slug', '=', $id_or_slug)->firstOrFail(); } }); There might be easier ways to do it... One problem with this is if there's a slug name that consists of only digits it will hit the route as id, not as a slug. In this case I guess you should add a validation rule that disallows slug names that are only numeric, when you're creating the resource - or change the slug to something.
I use this method a lot. However, I encountered an issue ModelNotFoundException error: No query results for the model. I don't know what went wrong yet I implemented the same thing in other parts of the application and it works very well. I get the error on my edit resource. I can't find what went wrong
Sir i have question . how to create rule validation array 2d [ [true,"10:00","11:00"],[false,"",""]] when true index[1][2] need some value , false don't need some value. sorry im bad english
Is it possible to Bind multiple? /category/{category:slug}/{sub_category:slug} my controller method: __invoke(Category $category, Category $sub_category) But I end up wit than error "App\Models\Category::subCategories()" The error is fixed with Route::bind but i'd love to know if it's possible without.
You probably need to have hasMany relationship in Category called exactly subCategories() Read this part of the docs: laravel.com/docs/9.x/routing#implicit-model-binding-scoping
Hi I have tried route building and see the query in laravel debugger it's making query select * from products where slug = product-1, how can I select specific columns only for example I only want 3 filed to show and want to select that particular fields only. Can we do so?
I was looking for how to change the route key, and you showed it, although it was the tiny unnecessary thing that either everybody knows well or nobody needs at all. Liked and subscribed at that moment :) Thank You!
Sir, how to solve the compact() string or array error in laravel 8 with php version 8.x. I recently came across this error but nowhere in internet found the solution. So I have to downgrade the php version to 7.x
@@LaravelDaily Sir this was the error: "ErrorException compact(): Argument #1 must be string or array of strings...." After I downgraded, it was resolved, though. But what if I want to keep php v 8.x?
Hard to comment without knowing what you actually passed into that compact(). Could you show the full code? Maybe you just passed null or non-existing variable?
Hi, I got 404 error after submitting the login form in laravel 8 auth. Once I registered with a new user it directly go to the dashboard and shows 404 error. When I remove that user from DB I am able to get the login page. So everytime when a user register it goes to dashboard and shows 404. error. That means, if there is no data in DB it will work. Please help me. I am new to this laravel. And I got stuck here
It's good to use but I think we can't use eager loading using show(Product $product)... I prefer to pass the $product_id and do $product = Product::with('category')->findOrFail($product_id)->get()...
Just to add context, route model binding only gets the model without relationship by default so we would not be able to use with() in this case, rather use load() method which works because load() is used to load relationship after the parent model has been queried.
Povilas, please create a video on using UUIDs instead of IDs in the database. I watched that video you uploaded but it is too old. And I think Laravel now supports UUIDs internally so that we don't need to use external packages. And can we only use UUID in the database table (without ID)?
Awesome content, I didn't know most of the details described on this video. I have a question, any help would be appreciated, is there any way to use route model binding with collection instead of single model? I'm trying to implement this feature with livewire in a project but I haven't any success yet😥
Yeah you specify it like normal: function functionName(Model $model) Then you break it down like: $model->where(conditions)->get() Or even just: $model->get() If your relationships are setup right Or, I believe you can just pass it to the view (if you need the collection in blade) and just put it directly like: return view('pages.page', [ 'model' => $model ]); Then in your blade you just do: Foreach ($model as $data) $data->name Endforeach
HI. I am a daily viewer of your channel. I am using your roles and permission system on my project. but I am using multi auth using guard for the separate user and admin session. Because I don't want to login as admin in the user front end. everything is fine. but it giving forbidden error with gate::denies() in the controller. I think it is coming for a guard. How do I check gate ability with auth guard ?? any method for it? or a different method for verifying this situation?
I don't actively work with multi guards so can't advice. And that is the exact reason why I don't work with multi guards, because it's hard to solve situations like the one you described.
I use roles/permissions for things like that. With different Gates or Middlewares. For my general approach, see video: ruclips.net/video/kZOgH3-0Bko/видео.html
I didn't think it was useful in practice for people to know what's under the hood, as they are not likely to change framework internals, but if you wanna know - here, I've googled an article 100r0bh.medium.com/demistifying-route-model-binding-in-laravel-a6e27e5a458e
@@LaravelDaily cool, thanks, that article clarified things a bit. So basically it's some "magic" methods for Resource controllers and in models which do all the work for you, and as you mentioned in your video you can overwrite them. Also at first I got it confused with Service Container logic which made me write the original comment. p.s. thanks for all the videos you make!
Well explained, I didn't know about the missing() function yet on routes. Hope you will get the 100k subscribers but I think you will need to extend your content to different languages and frameworks as the PHP market share has been in decline for a couple of years now. To put things in perspective, you already have more subscribers than the Laravel subreddit has so I doubt there is much more growth to gain.
Perfect content. This is almost assumed knowledge, but it's not something I ever came across when first delving into Laravel. Consequently I learned by osmosis which isn't ideal. I know you'll solve this problem for others with your new learning roadmap. Great stuff.
Great video. The missing method and changing route key for a specific route was new information for me. Thanks
Is there any solutions for route model binding with hasMany relationships? If a user has many products, auth()->user()->products()->findOrFail($product) works fine, but with route model binding, user can get access to other products. Middleware checking will create an extra load (double DB request).
something like with scopes but not.
Route HasMany binding for auth()->user()->products()->findOrFail($product);
Great content as always, the missing method it's a magic. I'm following your channel quite well from Africa/Dar es salaam. Keep up Good work
Excellent info thanks for sharing.
Great video Povilas, Thanks
Today I am extremely happy... IT have upgraded our LTS server to the latest SP which includes PHP 7.4, so I can finally start playing around with Laravel 8! Oh, and nice vid Povilas as always!
php artisan make:controller CategoryController --resource --model=Category can be shorted for php artisan make:controller CategoryController -r -m Category
Thanks
Thanks, keep going!
is the capital Email and Password affect in attempt method in laravel i add field name in camalcase in database.
if(Auth:: attempt (['Email'=>$request->email,'Password'=>$request->password]))
this will return always false if i used Email and Password instead of email and password.
can you tell me why this happening??
super perfect content
Thank you so much
Thank you, sir...
what if i need to add another condition like created_by = auth()->user()
Before watching the video: php reflection api is powerful enough.
i need show info , table product and category ,union how whit model binding?
Putting a resource controller inside grouped routes is not working
One more thing to add. If you want to bind a resource, nested resource or any route, you can you use scope as well. You can use it when you have multiple route model binding and you don't need to use the getRouteKeyName function
Sir awesome knowledge which you are sharing with us. Thanks for this. Sir i have question that what is alternative of id in url. And is it safe to show id in url.
Alternative is UUID.
Examples: laravelexamples.com/tag/ramsey-uuid
Old video: ruclips.net/video/TRDu1crA-g0/видео.html
Is it possible to cache the eloquent model?
i finish the LEVEL 1 and I am doing NOW the LEVEL 2 and this improve a lot my level THANK YOU SO MUCH
Thanks, for this awesome video.
Does the route model binding check policies or gates?
No.
@@LaravelDaily okay, thanks.
didn't know about 'missing'
While using laraveldaily invoice package it is throwing me an error saying target class [ ] doesn't exist how to solve this?
Open an issue on Github, with the exact code you've written, then one of my teammates may find time to help.
Please, put this lessons in a category
In what category?
Great
5:25 Can I get data using more than one database row? For example, id OR slug to bring the same result?
This is part of the docs that describe this behavior: laravel.com/docs/8.x/routing#customizing-the-resolution-logic
You can probably do something like this:
Route::get('/resource/{id_or_slug}', function ($id_or_slug) {
$isId = ctype_digit($id_or_slug);
if ($isId) { // find as id
return Resource::findOrFail($id_or_slug);
}
else // find as slug
{
return Resource::where('slug', '=', $id_or_slug)->firstOrFail();
}
});
There might be easier ways to do it...
One problem with this is if there's a slug name that consists of only digits it will hit the route as id, not as a slug. In this case I guess you should add a validation rule that disallows slug names that are only numeric, when you're creating the resource - or change the slug to something.
I use this method a lot. However, I encountered an issue ModelNotFoundException error: No query results for the model. I don't know what went wrong yet I implemented the same thing in other parts of the application and it works very well. I get the error on my edit resource. I can't find what went wrong
Sir i have question . how to create rule validation array 2d [ [true,"10:00","11:00"],[false,"",""]] when true index[1][2] need some value , false don't need some value. sorry im bad english
Is it possible to Bind multiple?
/category/{category:slug}/{sub_category:slug}
my controller method: __invoke(Category $category, Category $sub_category)
But I end up wit than error "App\Models\Category::subCategories()"
The error is fixed with Route::bind but i'd love to know if it's possible without.
You probably need to have hasMany relationship in Category called exactly subCategories()
Read this part of the docs: laravel.com/docs/9.x/routing#implicit-model-binding-scoping
@@LaravelDaily That worked, but I also realized that subcategory slugs are not unique so I had to manually query anyway.
Interesting
thanks
How can we load relationship data in route model binding scenario.
See this post: ruclips.net/user/postUgwq5jJmPqxjwPTdmr14AaABCQ
I love u
Hi
I have tried route building and see the query in laravel debugger it's making query select * from products where slug = product-1, how can I select specific columns only for example I only want 3 filed to show and want to select that particular fields only. Can we do so?
I was looking for how to change the route key, and you showed it, although it was the tiny unnecessary thing that either everybody knows well or nobody needs at all. Liked and subscribed at that moment :) Thank You!
⚘
Hey, In my model I have a pivot data type array, how I can perform cats on it ?
Sir, how to solve the compact() string or array error in laravel 8 with php version 8.x. I recently came across this error but nowhere in internet found the solution. So I have to downgrade the php version to 7.x
Can you show the exact error? I haven't seen any errors with compact and php 8
@@LaravelDaily Sir this was the error:
"ErrorException compact(): Argument #1 must be string or array of strings...."
After I downgraded, it was resolved, though. But what if I want to keep php v 8.x?
Hard to comment without knowing what you actually passed into that compact(). Could you show the full code? Maybe you just passed null or non-existing variable?
Hi,
I got 404 error after submitting the login form in laravel 8 auth.
Once I registered with a new user it directly go to the dashboard and shows 404 error. When I remove that user from DB I am able to get the login page.
So everytime when a user register it goes to dashboard and shows 404. error. That means, if there is no data in DB it will work.
Please help me. I am new to this laravel. And I got stuck here
Sorry I can't help you without debugging full code of your project, so can't answer in a RUclips comment.
It's good to use but I think we can't use eager loading using show(Product $product)... I prefer to pass the $product_id and do $product = Product::with('category')->findOrFail($product_id)->get()...
you can use $product->load('category') in your controller function.
Just to add context, route model binding only gets the model without relationship by default so we would not be able to use with() in this case, rather use load() method which works because load() is used to load relationship after the parent model has been queried.
First view first like ♥️♥️♥️♥️
what IDE is that?
I use phpstorm
if my slug looks like "/some/my/url"/ how do i can find model?
I wouldn't advise to use "/" in slugs cause then it's very confusing, and your question is a proof of it.
@@LaravelDaily ty, i'll but not in this project((
Route::get('/search/{search}', function (string $search) {
return $search;
})->where('search', '.*');
very helpfull thanks!
Thanks for your daily tips.
Povilas, please create a video on using UUIDs instead of IDs in the database. I watched that video you uploaded but it is too old. And I think Laravel now supports UUIDs internally so that we don't need to use external packages. And can we only use UUID in the database table (without ID)?
There will be some "surprise" related to UUIDs tomorrow on the channel :)
@@LaravelDaily ThankYou so much sir! You are doing great.
Can you do this with services?
Depends on what you mean by "this". You can always try and see if it works.
@@LaravelDaily I mean using model binding but having a service as well that processes more things like where clauses
Awesome content, I didn't know most of the details described on this video. I have a question, any help would be appreciated, is there any way to use route model binding with collection instead of single model? I'm trying to implement this feature with livewire in a project but I haven't any success yet😥
Yeah you specify it like normal:
function functionName(Model $model)
Then you break it down like:
$model->where(conditions)->get()
Or even just:
$model->get()
If your relationships are setup right
Or, I believe you can just pass it to the view (if you need the collection in blade) and just put it directly like:
return view('pages.page', [ 'model' => $model ]);
Then in your blade you just do:
Foreach ($model as $data)
$data->name
Endforeach
Thanks for the great video
is this work on lumen?
Looks very magically and I am not sure that is good...
That is not "magic". I wrote comment for better explonation
Real Gem of Laravel ❤
awesome tutorial
love it, thanks
excellent content
HI. I am a daily viewer of your channel. I am using your roles and permission system on my project. but I am using multi auth using guard for the separate user and admin session. Because I don't want to login as admin in the user front end. everything is fine. but it giving forbidden error with gate::denies() in the controller. I think it is coming for a guard. How do I check gate ability with auth guard ?? any method for it? or a different method for verifying this situation?
I don't actively work with multi guards so can't advice. And that is the exact reason why I don't work with multi guards, because it's hard to solve situations like the one you described.
@@LaravelDaily thanks for your reply
I use roles/permissions for things like that. With different Gates or Middlewares. For my general approach, see video: ruclips.net/video/kZOgH3-0Bko/видео.html
"... Laravel will magically find ... so how does it all work? ..." - didn't get an answer after watching this video :(
Internally it will be same as findOrFail model with the passed parameter. Hope this helps.
I didn't think it was useful in practice for people to know what's under the hood, as they are not likely to change framework internals, but if you wanna know - here, I've googled an article 100r0bh.medium.com/demistifying-route-model-binding-in-laravel-a6e27e5a458e
@@LaravelDaily cool, thanks, that article clarified things a bit. So basically it's some "magic" methods for Resource controllers and in models which do all the work for you, and as you mentioned in your video you can overwrite them. Also at first I got it confused with Service Container logic which made me write the original comment.
p.s. thanks for all the videos you make!
Well explained, I didn't know about the missing() function yet on routes. Hope you will get the 100k subscribers but I think you will need to extend your content to different languages and frameworks as the PHP market share has been in decline for a couple of years now. To put things in perspective, you already have more subscribers than the Laravel subreddit has so I doubt there is much more growth to gain.
I’m glad he didn’t listen to your advice. People like us appreciate his consistency in Laravel