Laravel Tip - Format Multilevel Categories With Single Query

แชร์
ฝัง
  • เผยแพร่เมื่อ 8 ก.พ. 2025
  • In this Laravel tip video, we will have a look at how to format a multi-level categories tree using only one query in Laravel.
    === Important Links ===
    Fonts, extensions I use, and Support Laratips links:
    bit.ly/m/laratips
    === Offer ===
    Laravel Hosting: platform.cloud...
    Get up to Free Hosting by using code "LARATIPS" and you get $15 as credit which can last almost a month.
    Cloudways offers peace of mind and flexibility so you can focus on growing your business and reaching new milestones instead of dealing with server management. With Cloudways, you get an optimized stack, managed servers, backups, staging environment, integrated Git, pre-configured, Composer, 24/7 support, and the choice of five cloud providers: AWS, DigitalOcean, Linode, Google Cloud, and Vultr
    ====================== Support ======================
    If you want me to continue making this kind of video for free on TH-cam, then you can support the channel
    eSewa ID: 9843632084
    Wise Link: bit.ly/3dnBRmX
    Wise email: ashish.dhamala2015@gmail.com
    ===
    GitHub repo:
    github.com/Lar...
    Format Multilevel Categories Using Only Has Many Relationship (Previous Video)
    • Laravel Tip - Format M...
    Social Media Links:
    Let's be friends
    / aashish_dhamala
    Follow On Facebook
    / laratipsofficial
    Tech stack:
    Laravel
    PHP
    #laravel #php #laraveltip #laratips

ความคิดเห็น • 66

  • @eatsfunk
    @eatsfunk 8 หลายเดือนก่อน

    Just discovered this channel. What a gift!

  • @itcforyou
    @itcforyou 2 ปีที่แล้ว

    Thanks!

    • @Laratips
      @Laratips  2 ปีที่แล้ว

      Thank you so much Mohamed for your support :)

  • @awaidus
    @awaidus ปีที่แล้ว +1

    Fantastic. That's what i'm looking for days. Thank you/

  • @md.anwarhossain1179
    @md.anwarhossain1179 ปีที่แล้ว

    Thanks a lot from Bangladesh for this more essential video❤️

  • @Z0dlk
    @Z0dlk ปีที่แล้ว

    Amazing video bro ty for share the knowledge .♥

  • @unlavab
    @unlavab 3 ปีที่แล้ว +1

    This is awesome!! Thanks mate

  • @cultureofnepal2024
    @cultureofnepal2024 3 ปีที่แล้ว

    Very useful tips bro. 👍👍👍

  • @mahbubhasan5905
    @mahbubhasan5905 2 ปีที่แล้ว +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 ?

  • @julienSibille
    @julienSibille 3 ปีที่แล้ว

    Wow really useful ! Many thanks !

  • @aboozark6307
    @aboozark6307 2 ปีที่แล้ว

    thanks a lot . it is very good.

  • @learnwithhabiburrahman
    @learnwithhabiburrahman ปีที่แล้ว

    Super excellent bro

  • @nishanthanr605
    @nishanthanr605 3 ปีที่แล้ว

    Very much Helpful

  • @GergelyCsermely
    @GergelyCsermely 3 ปีที่แล้ว

    Thanks nice solution!

    • @Laratips
      @Laratips  3 ปีที่แล้ว

      You're welcome!

  • @VinayakSoni98
    @VinayakSoni98 10 หลายเดือนก่อน

    thanks

  • @opahprincewill4103
    @opahprincewill4103 3 ปีที่แล้ว

    Thank you bro,. great tutorial. can on easily perform crud on these category?

    • @Laratips
      @Laratips  3 ปีที่แล้ว

      Yes, its not that difficult

  • @programar-tk6tb
    @programar-tk6tb 3 ปีที่แล้ว +1

    thank you very much ... could you plz make video for showing the tree in blade :)

  • @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.

  • @meetvaghasia6653
    @meetvaghasia6653 3 ปีที่แล้ว +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?

  • @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 ปีที่แล้ว +1

      I have created this video about showing it in blade part: th-cam.com/video/tewLdPba_5g/w-d-xo.html

  • @debjit21
    @debjit21 3 ปีที่แล้ว

    Please share the blade function/ CRUD example.

  • @relicverse
    @relicverse ปีที่แล้ว

    plz name of intro music

  • @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.
      th-cam.com/video/eHwIx0XDEUg/w-d-xo.html

  • @donatusbrainokwe4350
    @donatusbrainokwe4350 3 ปีที่แล้ว +1

    Please sir, how can we get the count of each category items with their subcategories. Anyone please

    • @Laratips
      @Laratips  3 ปีที่แล้ว +1

      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.

  • @bugszone6098
    @bugszone6098 3 ปีที่แล้ว +1

    great

  • @aidarark5558
    @aidarark5558 3 ปีที่แล้ว

    quality!

  • @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

  • @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 ปีที่แล้ว +1

      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

  • @ColantAlan
    @ColantAlan 3 ปีที่แล้ว +4

    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 ปีที่แล้ว +1

      @@ciprianserban You can use Model::getThree(); or Model::find(id)->children->children->children->...

  • @hammadmohd8336
    @hammadmohd8336 2 ปีที่แล้ว

    where is jquery

  • @Tekel-Upharsin
    @Tekel-Upharsin 3 ปีที่แล้ว

    I'm so used to seeing crap tech tutorials on TH-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.

    • @Laratips
      @Laratips  3 ปีที่แล้ว +1

      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 ปีที่แล้ว +4

    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 ปีที่แล้ว +5

      I have already made a video related to that. Here's the link: th-cam.com/video/2FYc2L7RUOI/w-d-xo.html
      But the problem with this is that, if we have 4 levels of categories, it will make 5 queries.

    • @nickamenthomson8874
      @nickamenthomson8874 2 ปีที่แล้ว

      Performance issue if N - 1 number of queries for N numbers of children

    • @ArshKhan-te9sz
      @ArshKhan-te9sz 2 ปีที่แล้ว

      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 ปีที่แล้ว +1

      @@vladimirbudkin1872 ic... ok thanks!

  • @xmaxh
    @xmaxh 11 หลายเดือนก่อน

    very poor, very slow and consumes too many resources, not functional