Laravel调用布尔值上的成员函数addEagerConstraint()



每当我进入用户页面时,我都会收到以下错误,它应该显示经过身份验证的用户是否已经关注了配置文件所在的用户。

这可能是关系设置的问题,它hasMany

堆栈跟踪

当地。错误:调用成员函数 addEagerConstraint(( on 布尔值 {"userId":1,"email":"fakeemail@aol.com","exception":"[对象] (Symfony\Component\Debug\Exception\FatalThrowableError(code: 0(: 调用成员函数 addEagerConstraint(( on boolean at/Applications/MAMP/htdocs/elipost/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php:522("} []

用户控制器.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){
return ['followedByMe' => $myuser->followers->count() == 0];
});

if (!$user) {
return redirect('404');
}
return view ('profile')->with('user', $user);
}

我的关注(模型(

<?php
class MyFollow extends Model
{
use SoftDeletes, CanFollow, CanBeFollowed;
protected $fillable = [
'user_id',
'followable_id'
];
public $timestamps = false;
protected $table = 'followables';
public function follower()
{
return $this->belongsTo('AppUser', 'followable_id');
}
public function followedByMe()
{
return $this->follower->getKey() === auth()->id();
}

}

我的关注

use OvertrueLaravelFollowTraitsCanFollow;
use OvertrueLaravelFollowTraitsCanBeFollowed;
class MyFollow extends Model
{
use SoftDeletes, CanFollow, CanBeFollowed;
protected $fillable = [
'user_id',
'followable_id'
];
public $timestamps = false;
protected $table = 'followables';
public function follower()
{
return $this->belongsTo('AppUser', 'followable_id');
}
public function followedByMe()
{
return $this->follower->getKey() === auth()->id();
}

}

发布

class Post extends Authenticatable
{

protected $fillable = [
'title',
'body',
'user_id',
'created_at',
];

public function user()
{
return $this->belongsTo(User::class);
}
public function comments()
{
return $this->hasMany('AppComment');
}
public function likes()
{
return $this->hasMany('AppLike');
}
public function likedByMe()
{
foreach($this->likes as $like) {
if ($like->user_id == auth()->id()){
return true;
}
}
return false;
}



}

喜欢

<?php
namespace App;
use AppPost;
use IlluminateDatabaseEloquentModel;
use IlluminateDatabaseEloquentSoftDeletes;
class Like extends Model
{
use SoftDeletes;
protected $fillable = [
'user_id',
'post_id'
];

}

用户(型号(

class User extends Authenticatable
{
use Notifiable,CanFollow, CanBeFollowed;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name', 'email', 'password',
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password', 'remember_token',
];

public function posts()
{
return $this->hasMany(Post::class);
}

public function images()
{
return $this->hasMany(GalleryImage::class, 'user_id');
}

public function likes()
{
return $this->hasMany('AppLike');
}
public function follow()
{   
return $this->hasMany('AppMyFollow');
}

public function comments()
{
return $this->hasMany('AppComment');
}


}

正如Jonas Staudenmeir所说,followedByMe不是一种关系,它是一个常规函数,它所做的是返回一个布尔值。我很困惑为什么你的用户模型有一个关注者并试图从关注者的关注者那里获取信息?只是简化,我在这里看到了太多不必要的急切加载。

按索引元素 (id( 搜索>按名称搜索,一周中的任何一天

编辑:

用户控制器

public function getProfile(Request $request, $id)
{  
//$request->user() will get you the authenticated user
$user = User::with(['posts.likes','followers','follows','followers.follows'])
->findOrFail($request->user()->id);
//This returns the authenticated user's information posts, likes, followers, follows and who follows the followers 
//If you wish to get someone else's information, you just switch 
//the $request->user()->id to the $id if you're working with id's, if you're
//working with names, you need to replace findOrFail($id) with ->where('name',$name')->get() and this will give you
//a collection, not a single user as the findOrFail. You will need to add a ->first() to get the first user it finds in the collection it results of
//If you're planning on getting an attribute (is_following = true) to know if
//the authenticated user is following, you can use an accessor in the User model and write this after you've fetched the instance of the User
//$user->append('is_following');
return view ('profile')->with('user', $user);
}

用户模型

//Accessor
//People who this user follows
public function getIsFollowingAttribute()
{   
return MyFollow::where('followable_id',$this->attributes['id'])->where('user_id',Auth()->user()->id)->count() > 0 ? true : false;
}
//Relationships
//People who this user follows
public function follow()
{   
return $this->hasMany('AppMyFollow','user_id','id');
}
//People who follows this user
public function followers()
{   
return $this->hasMany('AppMyFollow','followable_id','id');
}
//Posts of this user
public function posts()
{   
return $this->hasMany('AppPost','user_id','id');
}
//Likes of this user, not sure about this one tho, we're not using this for now but it could come in handy for you in the future
public function likes()
{   
return $this->hasManyThrough('AppLikes','AppPost','user_id','user_id','id');
}

帖子模型

//Who like this post
public function likes()
{   
return $this->hasMany('AppPost','user_id','id');
}

我的关注模型

//Relationships
//People who follow this user
public function followers()
{   
return $this->hasMany('AppMyFollow','followable_id','user_id');
}
//Relationships
//People who this user follows
public function follow()
{   
return $this->hasMany('AppMyFollow','user_id','followable_id');
}

在@abr的帮助下,我找到了一个简单的解决方案,简单的解决方案。

我的关注.php(模型(

public function followers()
{   
return $this->hasMany('AppMyFollow','followable_id','user_id');
}
//Relationships
//People who this user follows
public function follow()
{   
return $this->hasMany('AppMyFollow','user_id','followable_id');
}

用户.php(型号(

public function getIsFollowingAttribute()
{   
return MyFollow::where('followable_id',$this->attributes['id'])->where('user_id',Auth()->user()->id)->count() > 0 ? true : false;
}

public function follow()
{   
return $this->hasMany('AppMyFollow');
}

用户控制器.php

public function getProfile($user)
{  
$users = User::with(['posts.likes' => function($query) {
$query->whereNull('deleted_at');
$query->where('user_id', auth()->user()->id);
}, 'followers','follow.followers'])
->with(['followers' => function($query) {

}])->where('name','=', $user)->get();
$user = $users->map(function(User $myuser){
$myuser['followedByMe'] = $myuser->getIsFollowingAttribute();
return $myuser;
});

if(!$user){
return redirect('404');
}
return view ('profile')->with('user', $user);
}

它现在有效。:)

最新更新