Laravel Tip - Format Multilevel Categories With Single Query

  Рет қаралды 32,496

Laratips

Laratips

Күн бұрын

Пікірлер: 66
@eatsfunk
@eatsfunk 6 ай бұрын
Just discovered this channel. What a gift!
@awaidus
@awaidus Жыл бұрын
Fantastic. That's what i'm looking for days. Thank you/
@itcforyou
@itcforyou 2 жыл бұрын
Thanks!
@Laratips
@Laratips 2 жыл бұрын
Thank you so much Mohamed for your support :)
@md.anwarhossain1179
@md.anwarhossain1179 Жыл бұрын
Thanks a lot from Bangladesh for this more essential video❤️
@unlavab
@unlavab 3 жыл бұрын
This is awesome!! Thanks mate
@Z0dlk
@Z0dlk 11 ай бұрын
Amazing video bro ty for share the knowledge .♥
@mahbubhasan5905
@mahbubhasan5905 2 жыл бұрын
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 ?
@cultureofnepal2024
@cultureofnepal2024 3 жыл бұрын
Very useful tips bro. 👍👍👍
@julienSibille
@julienSibille 3 жыл бұрын
Wow really useful ! Many thanks !
@nishanthanr605
@nishanthanr605 3 жыл бұрын
Very much Helpful
@aboozark6307
@aboozark6307 Жыл бұрын
thanks a lot . it is very good.
@opahprincewill4103
@opahprincewill4103 3 жыл бұрын
Thank you bro,. great tutorial. can on easily perform crud on these category?
@Laratips
@Laratips 3 жыл бұрын
Yes, its not that difficult
@learnwithhabiburrahman
@learnwithhabiburrahman Жыл бұрын
Super excellent bro
@programar-tk6tb
@programar-tk6tb 3 жыл бұрын
thank you very much ... could you plz make video for showing the tree in blade :)
@Laratips
@Laratips 3 жыл бұрын
Yes, soon
@programar-tk6tb
@programar-tk6tb 3 жыл бұрын
@@Laratips thank you you are the best :)
@donatusbrainokwe4350
@donatusbrainokwe4350 3 жыл бұрын
@@Laratips Thanks please, soon 😊
@programar-tk6tb
@programar-tk6tb 3 жыл бұрын
@@Laratips :)
@programar-tk6tb
@programar-tk6tb 3 жыл бұрын
@@Laratips hi Boss
@irfanakram
@irfanakram 3 жыл бұрын
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.
@Laratips
@Laratips 3 жыл бұрын
Yes, I agree. For that you can use the first method with recursive relation. I have made a video about that before this one.
@GergelyCsermely
@GergelyCsermely 3 жыл бұрын
Thanks nice solution!
@Laratips
@Laratips 3 жыл бұрын
You're welcome!
@davidsonzed
@davidsonzed 2 жыл бұрын
Hi, can you show the blade part of this query to show how it will be working on a navigation of a website?
@Laratips
@Laratips 2 жыл бұрын
I have created this video about showing it in blade part: kzbin.info/www/bejne/qpbafZeGl8aSa8k
@relicverse
@relicverse Жыл бұрын
plz name of intro music
@debjit21
@debjit21 3 жыл бұрын
Please share the blade function/ CRUD example.
@meetvaghasia6653
@meetvaghasia6653 3 жыл бұрын
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?
@cdma2723
@cdma2723 3 жыл бұрын
How to add products_count foreach category level
@Laratips
@Laratips 3 жыл бұрын
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.
@cdma2723
@cdma2723 3 жыл бұрын
@@Laratips how to get sum of products_count in parent level
@donatusbrainokwe4350
@donatusbrainokwe4350 3 жыл бұрын
Please sir, how can we get the count of each category items with their subcategories. Anyone please
@Laratips
@Laratips 3 жыл бұрын
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.
@robyortega6147
@robyortega6147 2 жыл бұрын
can make crud example sir please this i need
@Laratips
@Laratips 2 жыл бұрын
You can check this video from my e-commerce series. I have done crud here. You will get nice idea from this video. kzbin.info/www/bejne/m3naeqtmjal4i8k
@ColantAlan
@ColantAlan 3 жыл бұрын
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...
@Laratips
@Laratips 3 жыл бұрын
Thanks for the comment. I have already made a video about that as well. This video is 2nd part of that video.
@ciprianserban
@ciprianserban 2 жыл бұрын
How can this be accessed in blade ?
@ColantAlan
@ColantAlan 2 жыл бұрын
@@ciprianserban You can use Model::getThree(); or Model::find(id)->children->children->children->...
@hammadmohd8336
@hammadmohd8336 Жыл бұрын
where is jquery
@turkialharbi9234
@turkialharbi9234 3 жыл бұрын
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
@Laratips
@Laratips 3 жыл бұрын
You can do $cat->children()->pluck("id")
@turkialharbi9234
@turkialharbi9234 3 жыл бұрын
@@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
@MetriWahyudi
@MetriWahyudi 3 жыл бұрын
when we have millions of categories, it will be long processing. need more efficient algorithm. maybe add "path" column
@Laratips
@Laratips 3 жыл бұрын
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?
@MetriWahyudi
@MetriWahyudi 3 жыл бұрын
@@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.
@stojankukrika7242
@stojankukrika7242 3 жыл бұрын
Can you make some gist or something it will be much useful then write from the video ;)
@Laratips
@Laratips 3 жыл бұрын
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
@VinayakSoni98
@VinayakSoni98 8 ай бұрын
thanks
@aidarark5558
@aidarark5558 3 жыл бұрын
quality!
@bugszone6098
@bugszone6098 2 жыл бұрын
great
@Tekel-Upharsin
@Tekel-Upharsin 3 жыл бұрын
I'm so used to seeing crap tech tutorials on KZbin 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.
@Laratips
@Laratips 3 жыл бұрын
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.
@mohamadcheaib
@mohamadcheaib 3 жыл бұрын
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
@Laratips
@Laratips 3 жыл бұрын
I have already made a video related to that. Here's the link: kzbin.info/www/bejne/aHe8lGWCbLeIhas But the problem with this is that, if we have 4 levels of categories, it will make 5 queries.
@nickamenthomson8874
@nickamenthomson8874 Жыл бұрын
Performance issue if N - 1 number of queries for N numbers of children
@ArshKhan-te9sz
@ArshKhan-te9sz Жыл бұрын
How to create three categories in this like category ->sub Category ->sub child category -> services/product
@محمدفرج-ث7ذ3د
@محمدفرج-ث7ذ3د 2 жыл бұрын
Oh what the bad code.
@Laratips
@Laratips 2 жыл бұрын
Thanks for the feedback. Can you plz send me good code for this?
@SEOng-gs7lj
@SEOng-gs7lj 3 жыл бұрын
i think you have many subqueries, so it is technically not a single query... use MPTT for a single query
@vladimirbudkin1872
@vladimirbudkin1872 3 жыл бұрын
Look at code better. He gets all records at first, then uses collection.
@SEOng-gs7lj
@SEOng-gs7lj 3 жыл бұрын
@@vladimirbudkin1872 ic... ok thanks!
@xmaxh
@xmaxh 10 ай бұрын
very poor, very slow and consumes too many resources, not functional
Quick and Easy Nested Categories in Laravel
20:58
Codecourse
Рет қаралды 16 М.
BAYGUYSTAN | 1 СЕРИЯ | bayGUYS
37:51
bayGUYS
Рет қаралды 859 М.
Mom Hack for Cooking Solo with a Little One! 🍳👶
00:15
5-Minute Crafts HOUSE
Рет қаралды 20 МЛН
Laravel Eloquent: Deeper Relationships with One Query
10:37
Laravel Daily
Рет қаралды 144 М.
Get Data From Multiple Databases In One Project | Laravel Tip
11:52
Laravel Security: Top 7 Mistakes Developers Make
11:16
Laravel Daily
Рет қаралды 87 М.
Show Multi Level Categories Using Blade Component
8:20
Laratips
Рет қаралды 11 М.
Eloquent: Query 3-Level Relations with hasManyThrough and withCount
7:36
New in Laravel 11 - Files Removed and Added
4:32
Laratips
Рет қаралды 3,5 М.
Laravel: Create Public API with Cache and Rate Limits
12:18
Laravel Daily
Рет қаралды 46 М.
Laravel Demo: Multi-Level Categories E-Shop Catalog
15:48
Laravel Daily
Рет қаралды 32 М.