Laravel Tip - Format Multilevel Categories With Single Query

Поділитися
Вставка
  • Опубліковано 2 січ 2025

КОМЕНТАРІ • 66

  • @eatsfunk
    @eatsfunk 6 місяців тому

    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❤️

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

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

  • @aboozark6307
    @aboozark6307 Рік тому

    thanks a lot . it is very good.

  • @nishanthanr605
    @nishanthanr605 3 роки тому

    Very much Helpful

  • @julienSibille
    @julienSibille 3 роки тому

    Wow really useful ! Many thanks !

  • @GergelyCsermely
    @GergelyCsermely 3 роки тому

    Thanks nice solution!

  • @learnwithhabiburrahman
    @learnwithhabiburrahman Рік тому

    Super excellent bro

  • @relicverse
    @relicverse Рік тому

    plz name of intro music

  • @opahprincewill4103
    @opahprincewill4103 3 роки тому

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

    • @Laratips
      @Laratips  3 роки тому

      Yes, its not that difficult

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

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

  • @programar-tk6tb
    @programar-tk6tb 3 роки тому +1

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

  • @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: ua-cam.com/video/tewLdPba_5g/v-deo.html

  • @debjit21
    @debjit21 3 роки тому

    Please share the blade function/ CRUD example.

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

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

  • @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.
      ua-cam.com/video/eHwIx0XDEUg/v-deo.html

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

  • @VinayakSoni98
    @VinayakSoni98 9 місяців тому

    thanks

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

  • @bugszone6098
    @bugszone6098 2 роки тому +1

    great

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

  • @aidarark5558
    @aidarark5558 3 роки тому

    quality!

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

  • @hammadmohd8336
    @hammadmohd8336 Рік тому

    where is jquery

  • @Tekel-Upharsin
    @Tekel-Upharsin 3 роки тому

    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.

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

    • @nickamenthomson8874
      @nickamenthomson8874 2 роки тому

      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 роки тому +1

      @@vladimirbudkin1872 ic... ok thanks!

  • @xmaxh
    @xmaxh 10 місяців тому

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