【问题标题】:laravel trying to get property of non-object from a model inside a controllerlaravel 试图从控制器内的模型中获取非对象的属性
【发布时间】: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关系在哪里-&gt;with(['followers'
  • 好问题,我不知道,我认为它应该是follow 对吧?
  • 首先followable_id是什么MyFollow模型?只是想知道MyFollow 模型中的字段是什么以及它们引用了哪个表

标签: php laravel


【解决方案1】:

followedByMe 错误地循环了一条记录。尝试以下更改:

public function followedByMe()
{
    return $this->follower->getKey() === auth()->id();
}

由于followerbelongsTo关系,所以最多只会返回一条记录,而不是集合。

map 函数也错误地在模型上使用数组访问。您不能在对象上使用['followedByMe'],要访问您需要使用-&gt; 表示法的属性,如$myuser-&gt;followedByMe。下面展示了如何使用地图功能:

$user = $users->map(function(User $myuser){
    return ['followedByMe' => $myuser->followers->count() == 0];
});

这会返回一个类似的数组:

[
    ['followedByMe' => true],
    ['followedByMe' => false],
    ['followedByMe' => true],
]

【讨论】:

  • 我得到Call to a member function addEagerConstraints() on boolean,我需要确定是真是假。
  • 我仍然收到Call to a member function addEagerConstraints() on boolean
  • 堆栈跟踪和/或行号等更多信息会有所帮助。
  • $relation = $this-&gt;getRelation($name); $relation-&gt;addEagerConstraints($models); $constraints($relation);
  • 这有帮助吗?
猜你喜欢
  • 2014-08-13
  • 1970-01-01
  • 2019-03-13
  • 2019-06-30
  • 2018-02-21
  • 2015-09-22
  • 2017-03-20
  • 2014-03-25
  • 2015-01-25
相关资源
最近更新 更多