If we are in a child catagory. We like to go back in parent category then how we can show ? Like i am in 4 number sub catagory.. the how i can show 4》3》2》1》0 ?
Amazing...🔥🔥 Can you make one more video for shown this categories in front end side with pagination with orderBy name in each categories and their subcategory of available A AA AAA AAAA AAB AB AC B C D.... Like that... I am able to show this like tree structure without pagination, but how to do with pagination?
This is very good but i have one problem in this way, like orderBy or paginate is not working in this way, if you have another way to implement paginate.
Yes, you have to write withCount("children") when getting the categories in the begining in the query builder. Then you will be able to get the children count.
When getting all the categories in the beginning, you can add withCount(["products"]) in the query. For this to work you need to have products relation method defined in the category model.
You can check this video from my e-commerce series. I have done crud here. You will get nice idea from this video. ua-cam.com/video/eHwIx0XDEUg/v-deo.html
You can use nested with on your relation like this: public function children(): HasMany { return $this->hasMany(Category::class, 'parent_id', 'id')->with('children'); } public static function getThree(): array { return Category::whereNull('parent_id')->with('children')->get()->toArray(); } This solution avoid the n+1 query problem. If you have 3 nested, only 3 sql requests...
Yes, it's impractical to get all the records if there are millions of categories. Can you explain a little bit more on it? How can we benefit from the "path" column?
@@Laratips | id | name | path | parent_id ___________________________________________________ | 1 | cat A | null | null | 2 | cat B | 1/ | 1 | 3 | cat C | 1/2/ | 2 | 4 | cat D | 1/2/3/ | 3 | 5 | cat E | 1/2/ | 2 when you want to retrieve id=1 with all its descendants: SELECT * FROM table WHERE id = 1 or path regxp '(1\/)'; laravel way: $result = DB::table('table')->where('id',1)-orWhere('path','regexp','(1\/)')->get(); then convert the $result to tree structure like what you made in this video. using all() is very heavy when dealing with lot of data.
I forgot to add the link of the github repo in the description. Here it is github.com/Laratipsofficial/tips-and-tricks/tree/multilevel-category-with-one-query
I'm so used to seeing crap tech tutorials on UA-cam by people with Asian accents that I almost immediately closed this. I'm glad I didn't. I was being prejudice without realizing, and for that I apologize.
You don't have to apologize for this. Also glad that you liked the video. And also I am very happy that I became the one who made you change your mind on this 😉. I just want to bring what I have learned (from basic to advanced) to all people who want to learn.
Why you dont use eloquent relation hasMany and repeat the relation instead of function recursive. Try: public function children() { return $this→hasMany(Category::class, "parent_id")→with(["children"=› function($query){ return $query-›with("children"); }]) } Thats it! Few lines
I have already made a video related to that. Here's the link: ua-cam.com/video/2FYc2L7RUOI/v-deo.html But the problem with this is that, if we have 4 levels of categories, it will make 5 queries.
Just discovered this channel. What a gift!
Thanks!
Thank you so much Mohamed for your support :)
Fantastic. That's what i'm looking for days. Thank you/
Thanks a lot from Bangladesh for this more essential video❤️
If we are in a child catagory. We like to go back in parent category then how we can show ? Like i am in 4 number sub catagory.. the how i can show 4》3》2》1》0 ?
Amazing video bro ty for share the knowledge .♥
This is awesome!! Thanks mate
Very useful tips bro. 👍👍👍
thanks a lot . it is very good.
Very much Helpful
Wow really useful ! Many thanks !
Thanks nice solution!
You're welcome!
Super excellent bro
plz name of intro music
Thank you bro,. great tutorial. can on easily perform crud on these category?
Yes, its not that difficult
Amazing...🔥🔥
Can you make one more video for shown this categories in front end side with pagination with orderBy name in each categories and their subcategory of available
A
AA
AAA
AAAA
AAB
AB
AC
B
C
D....
Like that...
I am able to show this like tree structure without pagination, but how to do with pagination?
This is very good but i have one problem in this way, like orderBy or paginate is not working in this way, if you have another way to implement paginate.
Yes, I agree. For that you can use the first method with recursive relation. I have made a video about that before this one.
thank you very much ... could you plz make video for showing the tree in blade :)
Yes, soon
@@Laratips thank you you are the best :)
@@Laratips Thanks please, soon 😊
@@Laratips :)
@@Laratips hi Boss
Hi, can you show the blade part of this query to show how it will be working on a navigation of a website?
I have created this video about showing it in blade part: ua-cam.com/video/tewLdPba_5g/v-deo.html
Please share the blade function/ CRUD example.
Please sir, how can we get the count of each category items with their subcategories. Anyone please
Yes, you have to write withCount("children") when getting the categories in the begining in the query builder. Then you will be able to get the children count.
How to add products_count foreach category level
When getting all the categories in the beginning, you can add withCount(["products"]) in the query. For this to work you need to have products relation method defined in the category model.
@@Laratips how to get sum of products_count in parent level
can make crud example sir please this i need
You can check this video from my e-commerce series. I have done crud here. You will get nice idea from this video.
ua-cam.com/video/eHwIx0XDEUg/v-deo.html
You can use nested with on your relation like this:
public function children(): HasMany
{
return $this->hasMany(Category::class, 'parent_id', 'id')->with('children');
}
public static function getThree(): array
{
return Category::whereNull('parent_id')->with('children')->get()->toArray();
}
This solution avoid the n+1 query problem. If you have 3 nested, only 3 sql requests...
Thanks for the comment. I have already made a video about that as well. This video is 2nd part of that video.
How can this be accessed in blade ?
@@ciprianserban You can use Model::getThree(); or Model::find(id)->children->children->children->...
thanks
how to get all ids of children using find method not get ? example: $cat = Category::Find( $id ); and then get all children ids of this category
You can do $cat->children()->pluck("id")
@@Laratips it didn't work it will get one level of children can you tell how to do that with the same example in this video
great
when we have millions of categories, it will be long processing. need more efficient algorithm.
maybe add "path" column
Yes, it's impractical to get all the records if there are millions of categories.
Can you explain a little bit more on it? How can we benefit from the "path" column?
@@Laratips
| id | name | path | parent_id
___________________________________________________
| 1 | cat A | null | null
| 2 | cat B | 1/ | 1
| 3 | cat C | 1/2/ | 2
| 4 | cat D | 1/2/3/ | 3
| 5 | cat E | 1/2/ | 2
when you want to retrieve id=1 with all its descendants:
SELECT * FROM table WHERE id = 1 or path regxp '(1\/)';
laravel way:
$result = DB::table('table')->where('id',1)-orWhere('path','regexp','(1\/)')->get();
then convert the $result to tree structure like what you made in this video.
using all() is very heavy when dealing with lot of data.
quality!
Can you make some gist or something it will be much useful then write from the video ;)
I forgot to add the link of the github repo in the description. Here it is
github.com/Laratipsofficial/tips-and-tricks/tree/multilevel-category-with-one-query
where is jquery
I'm so used to seeing crap tech tutorials on UA-cam by people with Asian accents that I almost immediately closed this. I'm glad I didn't. I was being prejudice without realizing, and for that I apologize.
You don't have to apologize for this. Also glad that you liked the video. And also I am very happy that I became the one who made you change your mind on this 😉. I just want to bring what I have learned (from basic to advanced) to all people who want to learn.
Why you dont use eloquent relation hasMany and repeat the relation instead of function recursive.
Try:
public function children()
{
return $this→hasMany(Category::class, "parent_id")→with(["children"=› function($query){
return $query-›with("children");
}])
}
Thats it! Few lines
I have already made a video related to that. Here's the link: ua-cam.com/video/2FYc2L7RUOI/v-deo.html
But the problem with this is that, if we have 4 levels of categories, it will make 5 queries.
Performance issue if N - 1 number of queries for N numbers of children
How to create three categories in this like category ->sub Category ->sub child category -> services/product
Oh what the bad code.
Thanks for the feedback. Can you plz send me good code for this?
i think you have many subqueries, so it is technically not a single query... use MPTT for a single query
Look at code better. He gets all records at first, then uses collection.
@@vladimirbudkin1872 ic... ok thanks!
very poor, very slow and consumes too many resources, not functional