【发布时间】:2018-12-16 16:27:27
【问题描述】:
所以我试图检查一个经过身份验证的用户是否已经在关注该用户,但是我得到了这个错误。
试图获取非对象的属性
if ($followers->user_id == auth()->id()){ 返回真; }
8 "试图获取非对象的属性" "/Applications/MAMP/htdocs/elipost/app/MyFollow.php" 34
我不确定我是否正确使用下面的这种方法。
$query->where('user_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;
});
用户.php
public function follow()
{
return $this->hasMany('App\MyFollow');
}
我的关注(模型)
<?php
namespace App;
use App\User;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Overtrue\LaravelFollow\Traits\CanFollow;
use Overtrue\LaravelFollow\Traits\CanBeFollowed;
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()
{
foreach($this->follower as $followers) {
if ($followers->user_id == auth()->id()){
return true;
}
}
return false;
}
}
【问题讨论】:
-
能否请您显示您的堆栈跟踪?
-
我刚刚更新了它
-
你使用的
followers关系在哪里->with(['followers'? -
好问题,我不知道,我认为它应该是
follow对吧? -
首先
followable_id是什么MyFollow模型?只是想知道MyFollow模型中的字段是什么以及它们引用了哪个表