php - Laravel Eloquent returns 500 when I'm trying to put children in $with -
here eloquent code:
class testtype extends model { protected $with = ['parent', 'children']; public function user(){ return $this->belongsto('app\user'); } public function parent(){ return $this->belongsto('app\models\testtype', 'parent_id', 'id'); } public function children(){ return $this->hasmany('app\models\testtype', 'parent_id', 'id'); } }
eloquent has children , parent calls itself. returns http error 500. works remove children $with
it works when call controller:
$testtype = testtype::where('id', 1)->first(); return $testtype->children;
can me please?
if want children in response this
$testtype = testtype::with('children',function($query){ $query->where('id', 1);})->first();
do check getting using dd()
, make sure have specified relationships using foreign keys in migrations
i hope work if doesn't feel free shoot query. :)
Comments
Post a Comment