代码之家  ›  专栏  ›  技术社区  ›  BARNOWL

当用户没有跟随者时尝试获取非对象错误的属性

  •  0
  • BARNOWL  · 技术社区  · 6 年前

    每当我转到用户配置文件页,并且用户没有关注者时,就会出现以下错误。 我在用 laravel follow

    试图获取非对象的属性 $user->关注者->获取();

    当用户有关注者时,它会显示没有错误的关注者。

    myfollow.php文件

    class MyFollow extends Model
    {
        use SoftDeletes, CanFollow, CanBeFollowed;
    
        protected $fillable = [
            'user_id',
            'followable_id'
        ];
    
        public $timestamps = false;
    
        protected $table = 'followables';
    
        public function followers()
        {
            $user = User::find($this->user_id); 
    
            $user->followers->get();
    
    
        }
    
    
    
    }
    

    usercontroller.php用户控制器

    public function getProfile($user)
    {  
            $user = User::with(['posts.likes' => function($query) {
                                $query->whereNull('deleted_at');
                            }])
                          ->where('name','=', $user)
    
                          ->with(['follow' => function($query) {
    
                                $query->with('followers');
    
                           }])->first();
    
    
            if(!$user){
                return redirect('404');
            }
    
            return view ('profile')->withUser($user);
    }
    

    profile.blade.php文件

                @foreach($user->followers as $use)
                        @isset($use->name)
                        <ul>
    
                            <li>{{$use->name}}</li>
                         @endisset
                        </ul>
    
    
    
                @endforeach
    

    user.php用户

      public function follow()
        {   
            return $this->hasMany('App\MyFollow');
        }
    
    2 回复  |  直到 6 年前
        1
  •  1
  •   rkj    6 年前

    MyFollow模型

    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 getProfile($user)
    {  
            $user = User::with(['posts.likes' => function($query) {
                                $query->whereNull('deleted_at');
                            }, 'follow','follow.follower'])
                          ->where('name','=', $user)->first();
    
    
            if(!$user){
                return redirect('404');
            }
    
            return view ('profile')->with('user', $user);
    }
    

    查看

    @foreach($user->follow as $follow)
         <ul>
            @if($follow->follower)
               <li>{{$follow->follower->name}}</li>
            @endif
        </ul>
    @endforeach
    
        2
  •  0
  •   mankowitz    6 年前

    所以,在返回一个值之前,只需要做一个健全的检查。

    public function followers()
    {
        $user = User::find($this->user_id); 
        if ($user->followers) 
        { 
            return $user->followers->get(); 
        } else {  
            return "No Followers"; 
        }
    }
    

    根据追随者的构成方式,您可能需要 $user->followers->count()