AliBayat / Laravel-Categorizable

Implementing Categories system for Laravel's Eloquent models.
MIT License
60 stars 10 forks source link

parent eager loading problem #5

Closed kiansdi closed 2 years ago

kiansdi commented 2 years ago

Hello , I appreciate your useful package and thank you. I have a problem with parent relationship eager loading... when I want to get category instances with their parents in eager loading way I get nothing in parent key returned from eloquent. categorizable

AliBayat commented 2 years ago

@kiansdi

that's how the package will behave if there are no parents:

the exact command you used will return the below results:

       App\Models\Category {#5282
         id: 7,
         name: "PHP",
         slug: "php",
         type: "default",
         _lft: 13,
         _rgt: 16,
         parent_id: null,
         created_at: "2021-08-05 13:03:40",
         updated_at: "2021-08-05 13:03:40",
         parent: null,
       },
       App\Models\Category {#5283
         id: 8,
         name: "Laravel",
         slug: "laravel",
         type: "default",
         _lft: 14,
         _rgt: 15,
         parent_id: 7,
         created_at: "2021-08-05 13:03:40",
         updated_at: "2021-09-07 10:03:18",
         parent: App\Models\Category {#5276
           id: 7,
           name: "PHP",
           slug: "php",
           type: "default",
           _lft: 13,
           _rgt: 16,
           parent_id: null,
           created_at: "2021-08-05 13:03:40",
           updated_at: "2021-08-05 13:03:40",
         },
       },

did you correctly append the child category?

AliBayat commented 2 years ago

as mentioned before, you wont be able to fetch a relationship if it doesnt exist