Php laravel试图从控制器内的模型获取非对象的属性
因此,我试图检查一个经过身份验证的用户是否已经在跟踪该用户,但是我收到了这个错误 正在尝试获取非对象的属性 如果($followers->user\u id==auth()->id()){ 返回true; } 8“尝试获取非对象的属性” “/Applications/MAMP/htdocs/elipost/app/MyFollow.php”34 我不确定我是否正确使用下面的方法Php laravel试图从控制器内的模型获取非对象的属性,php,laravel,Php,Laravel,因此,我试图检查一个经过身份验证的用户是否已经在跟踪该用户,但是我收到了这个错误 正在尝试获取非对象的属性 如果($followers->user\u id==auth()->id()){ 返回true; } 8“尝试获取非对象的属性” “/Applications/MAMP/htdocs/elipost/app/MyFollow.php”34 我不确定我是否正确使用下面的方法 $query->where('user\u id',auth()->user()->id) UserControlle
$query->where('user\u id',auth()->user()->id)代码>
UserController.php
public function getProfile($user)
{
$users = User::with(['posts.likes' => function($query) {
$query->whereNull('deleted_at');
$query->where('user_id', auth()->user()->id);
}, 'follow','follow.follower'])
->with(['followers' => function($query) {
$query->with('follow.followedByMe');
$query->where('user_id', auth()->user()->id);
}])->where('name','=', $user)->get();
$user = $users->map(function(User $myuser){
$myuser['followedByMe'] = $myuser->followers->count() == 0 ? false : true;
// $myuser['followedByMe'] = $myuser->followers->count() == 0 ? false : true;
dd($owl = $myuser['followedByMe']);
return $myuser;
});
public function follow()
{
return $this->hasMany('App\MyFollow');
}
User.php
public function getProfile($user)
{
$users = User::with(['posts.likes' => function($query) {
$query->whereNull('deleted_at');
$query->where('user_id', auth()->user()->id);
}, 'follow','follow.follower'])
->with(['followers' => function($query) {
$query->with('follow.followedByMe');
$query->where('user_id', auth()->user()->id);
}])->where('name','=', $user)->get();
$user = $users->map(function(User $myuser){
$myuser['followedByMe'] = $myuser->followers->count() == 0 ? false : true;
// $myuser['followedByMe'] = $myuser->followers->count() == 0 ? false : true;
dd($owl = $myuser['followedByMe']);
return $myuser;
});
public function follow()
{
return $this->hasMany('App\MyFollow');
}
MyFollow(型号)
followedByMe
错误地循环单个记录。尝试以下更改:
public function followedByMe()
{
return $this->follower->getKey() === auth()->id();
}
由于follower
是一个belongsTo
关系,因此它最多只返回一条记录,而不是一个集合
map
函数也错误地在模型上使用数组访问。不能在对象上使用['followedByMe']
来访问需要使用->
符号的属性,如$myuser->followedByMe
中所示。下面显示了如何使用映射功能:
$user = $users->map(function(User $myuser){
return ['followedByMe' => $myuser->followers->count() == 0];
});
这将返回一个类似以下内容的数组:
[
['followedByMe' => true],
['followedByMe' => false],
['followedByMe' => true],
]
你能显示你的堆栈跟踪吗?我刚刚更新了它。你使用的->跟的关系在哪里(['followers'
?好问题,我不知道,我想它应该是follow
对吧?首先,在MyFollow
模型中,什么是follow\u id
?只想知道MyFollow
模型中的字段是什么,它们引用了哪个表调用成员函数addagerConstraints()在boolean上,我需要确定是真是假。我仍然得到对boolean上的成员函数AddagerConstraints()的调用更多信息,例如堆栈跟踪和/或行号,这会很有帮助。$relation=$this->getRelation($name);$relation->AddagerConstraints($models);$constraints($relation);
这有帮助吗?