Laravel recursive relationship return error in query

Viewed 47

i have model Hobbies with these relationship

public function sub_hobbies(){
    return $this->hasMany(Hobbies::class, 'parent_id')->with('media');
}

public function allsub(){
    return $this->sub_hobbies()->with('allsub');
}

in my table hobbies i have data like this

id     name       parent_id
1      hobbie 1   null
2      hobbie 2   1
3      hobbie 3   2

with my query Hobbies::with('sub_hobbies')->where('parent_id', null)->get(); i get the hobbies with sub_hobbies but there is the third stage, so when using Hobbies::with('allsub')->where('parent_id', null)->get(); i except to have 3 stage of hobbies but i got error 500 without error message

i'm in laravel 8.75.0

EDIT I found in my log that it make infinite sqlquery, i don't know why it behave like this, i use Mysql if there is something to do with it

0 Answers
Related