dqan70724 2018-07-09 03:28
浏览 172
已采纳

laravel试图从控制器内的模型获取非对象的属性

So im trying to check if an authenticated user is already following the user, however im getting this error.

Trying to get property of non-object

if ($followers->user_id == auth()->id()){ return true; }

8 "Trying to get property of non-object" "/Applications/MAMP/htdocs/elipost/app/MyFollow.php" 34

I'm not sure if im using this method below properly.

$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;

    });

User.php

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

MyFollow(model)

<?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;
    }


}
  • 写回答

1条回答 默认 最新

  • douleng3463 2018-07-09 03:45
    关注

    followedByMe is incorrectly looping a single record. Try the following changes:

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

    Since follower is a belongsTo relationship, it will only return at most one record, not a collection.

    The map function is also incorrectly using array access on a model. You cannot use ['followedByMe'] on an object, to access a property you need to use -> notation as in $myuser->followedByMe. The following shows how to use the map function:

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

    Which would return an array similar to:

    [
        ['followedByMe' => true],
        ['followedByMe' => false],
        ['followedByMe' => true],
    ]
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题