Laravel get Eloquent relation by same name as its attribute

后端 未结 3 1365
攒了一身酷
攒了一身酷 2021-01-18 11:44

I have database tables like this:

shoot: id, name, programme
programme: id, name

The eloquent relationship in the shoot is defined like thi

3条回答
  •  悲&欢浪女
    2021-01-18 12:35

    To achieve what you after you will need to do the following:

    $shoot = Shoot:where('id','=',1)->with('programme')->first();
    $variable = $shoot->programme; // returns 1
    $obj = $page->getRelationValue('programme') // returns App\Programme object.
    

提交回复
热议问题