Php 对布尔函数上的成员函数addEagerConstraints()的Laravel调用

Php 对布尔函数上的成员函数addEagerConstraints()的Laravel调用,php,laravel,Php,Laravel,每当我进入一个用户页面时,就会出现以下错误,它应该显示经过身份验证的用户是否已经在跟踪配置文件所在的用户 这可能是关系设置的问题吗,它有很多 堆栈跟踪 local.ERROR:调用上的成员函数AddagerConstraints() 布尔值{“userId”:1,“email”:fakeemail@aol.com“,”异常“:“[对象] (Symfony\Component\Debug\Exception\FatalThroTableError(代码:0): 对位于的布尔值调用成员函数addag

每当我进入一个用户页面时,就会出现以下错误,它应该显示经过身份验证的用户是否已经在跟踪配置文件所在的用户

这可能是关系设置的问题吗,它有很多

堆栈跟踪

local.ERROR:调用上的成员函数AddagerConstraints() 布尔值{“userId”:1,“email”:fakeemail@aol.com“,”异常“:“[对象] (Symfony\Component\Debug\Exception\FatalThroTableError(代码:0): 对位于的布尔值调用成员函数addagerConstraints() /Applications/MAMP/htdocs/elipost/vendor/laravel/framework/src/illusted/Database/elount/Builder.php:522)“} []

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


    if (!$user) {
        return redirect('404');
    }

    return view ('profile')->with('user', $user);
}
 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);
    }
MyFollow(型号)

发布

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('App\Comment');
    }

    public function likes()
    {
         return $this->hasMany('App\Like');
    }

    public function likedByMe()
    {
        foreach($this->likes as $like) {
            if ($like->user_id == auth()->id()){
                return true;
            }
        }
        return false;
    }






}
喜欢

<?php

namespace App;

use App\Post;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Like extends Model
{
    use SoftDeletes;

     protected $fillable = [
        'user_id',
        'post_id'
    ];



}

正如Jonas Staudenmeir所说,followedByMe不是一个关系,它是一个正则函数,它的作用是返回一个布尔值。我很困惑,为什么你有一个跟随你的用户模型,并试图从跟随者那里获得信息?简单一点,我看到这里有太多不必要的急于加载

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

编辑:

UserController

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);
}
用户模型

<?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('App\User', 'followable_id');
    }

    public function followedByMe()
    {
      return $this->follower->getKey() === auth()->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('App\Like');
    }

    public function follow()
    {   
        return $this->hasMany('App\MyFollow');
    }



    public function comments()
    {
        return $this->hasMany('App\Comment');
    }




}
//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('App\MyFollow','user_id','id');
}
//People who follows this user
public function followers()
{   
    return $this->hasMany('App\MyFollow','followable_id','id');
}
//Posts of this user
public function posts()
{   
    return $this->hasMany('App\Post','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('App\Likes','App\Post','user_id','user_id','id');
}
//Who like this post
public function likes()
{   
    return $this->hasMany('App\Post','user_id','id');
}
//Relationships
//People who follow this user
public function followers()
{   
    return $this->hasMany('App\MyFollow','followable_id','user_id');
}
//Relationships
//People who this user follows
public function follow()
{   
    return $this->hasMany('App\MyFollow','user_id','followable_id');
}
public function followers()
{   
    return $this->hasMany('App\MyFollow','followable_id','user_id');
}
//Relationships
//People who this user follows
public function follow()
{   
    return $this->hasMany('App\MyFollow','user_id','followable_id');
}
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('App\MyFollow');
}
后期模型

<?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('App\User', 'followable_id');
    }

    public function followedByMe()
    {
      return $this->follower->getKey() === auth()->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('App\Like');
    }

    public function follow()
    {   
        return $this->hasMany('App\MyFollow');
    }



    public function comments()
    {
        return $this->hasMany('App\Comment');
    }




}
//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('App\MyFollow','user_id','id');
}
//People who follows this user
public function followers()
{   
    return $this->hasMany('App\MyFollow','followable_id','id');
}
//Posts of this user
public function posts()
{   
    return $this->hasMany('App\Post','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('App\Likes','App\Post','user_id','user_id','id');
}
//Who like this post
public function likes()
{   
    return $this->hasMany('App\Post','user_id','id');
}
//Relationships
//People who follow this user
public function followers()
{   
    return $this->hasMany('App\MyFollow','followable_id','user_id');
}
//Relationships
//People who this user follows
public function follow()
{   
    return $this->hasMany('App\MyFollow','user_id','followable_id');
}
public function followers()
{   
    return $this->hasMany('App\MyFollow','followable_id','user_id');
}
//Relationships
//People who this user follows
public function follow()
{   
    return $this->hasMany('App\MyFollow','user_id','followable_id');
}
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('App\MyFollow');
}
MyFollow车型

<?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('App\User', 'followable_id');
    }

    public function followedByMe()
    {
      return $this->follower->getKey() === auth()->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('App\Like');
    }

    public function follow()
    {   
        return $this->hasMany('App\MyFollow');
    }



    public function comments()
    {
        return $this->hasMany('App\Comment');
    }




}
//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('App\MyFollow','user_id','id');
}
//People who follows this user
public function followers()
{   
    return $this->hasMany('App\MyFollow','followable_id','id');
}
//Posts of this user
public function posts()
{   
    return $this->hasMany('App\Post','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('App\Likes','App\Post','user_id','user_id','id');
}
//Who like this post
public function likes()
{   
    return $this->hasMany('App\Post','user_id','id');
}
//Relationships
//People who follow this user
public function followers()
{   
    return $this->hasMany('App\MyFollow','followable_id','user_id');
}
//Relationships
//People who this user follows
public function follow()
{   
    return $this->hasMany('App\MyFollow','user_id','followable_id');
}
public function followers()
{   
    return $this->hasMany('App\MyFollow','followable_id','user_id');
}
//Relationships
//People who this user follows
public function follow()
{   
    return $this->hasMany('App\MyFollow','user_id','followable_id');
}
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('App\MyFollow');
}

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

MyFollow.php(模型)

User.php(模型)

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


    if (!$user) {
        return redirect('404');
    }

    return view ('profile')->with('user', $user);
}
 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);
    }

现在可以用了。:)

奇怪的是,你正在链接
::with(…)->with(…)
;您只需执行以下操作:([“一”=>函数($q){…},“二”=>函数($q){…},“三”,“四”,…])。我认为这不会引起问题,因为您可以链接方法,例如
->where()
,但它看起来不太合适。
followedByMe
不是一种关系。因此,您不能将
$query->与('follow.followedByMe')
一起使用,试图获取非对象的属性(View:/Applications/MAMP/htdocs/elipost/resources/views/profile.blade.php)
它会弄乱$user变量的结构,你能推荐一些其他的吗?我没有在后面加上你的剪报,让我试一试。是的,它不起作用,只是更多的错误,我认为它现在的设置方式很好,我认为还有一些问题,可能是模型,你的代码破坏了我这边的一切。给我你表的模式(MyFollows、Post、Likes和User)我将编辑我的帖子(用它编辑你的帖子)