0
我在应用程序\用户模型文件中的以下方法获取用户相关建筑
Public function buildings(){
return Building::whereHas('area', function ($q) {
$q->whereHas('users', function ($q) {
$q->where('users.id', auth()->id());
});
})->get();
}
当我试图让在查看文件中的用户的建筑物,我得到这个错误:
@foreach (Auth::user()->buildings as $b)
{{$b->name}}
@endforeach
App \ User :: buildings必须返回关系实例。 (查看:C:\ xampp \ htdocs \ hse \ resources \ views \ observations \ form_observation.blade.php)
我试过了web.php中的代码,它的工作原理意味着关系设置是完美的,但是我希望它在用户模型
Route::get('/', function() {
return App\Building::whereHas('area', function ($q) {
$q->whereHas('users', function ($q){
$q->where('users.id',11);
});
})->get();
});
看看这个问题[laravel - 为什么函数调用没有括号(https://stackoverflow.com/questions/26512186/laravel-why-function-call-with-no -括号) – ljubadr