Laravel错误:“此集合实例上不存在属性[兄弟姐妹]”

时间:2018-11-24 10:42:13

标签: laravel eloquent

我想获取每个学生拥有的兄弟姐妹列表

控制器:

public function show($id)
{
    $finddata = Student::orderBy('id', 'asc')->get();
    $BS = Student::with('Family')->get();

    return View('students.list' ,compact('finddata','BS'));
}

学生模型:外键:family_id

public function family()
{
    return $this->belongsTo(family::class, 'family_id');
}

public function siblings()
{
    return $this->belongsTo("Family", "family_id");
}

家庭模式:

public function siblings()
{
    return $this->hasMany("Student");
}

查看:

@foreach($finddata as $record)
    {{$finddata->studentname}}
@endforeach

@foreach($BS->siblings as sibling)    
{{sibling->name}}
@endforeach
  

错误:“此集合实例上不存在[兄弟姐妹]属性。(视图:\ views \ students \ list.blade.php)”

0 个答案:

没有答案