【问题标题】:Property 'follows' not found in Illuminate\Contracts\Auth\Authenticatable|null在 Illuminate\Contracts\Auth\Authenticatable|null 中找不到属性“跟随”
【发布时间】:2021-01-08 16:15:22
【问题描述】:

出现给我的错误“ErrorException 试图获取非对象的属性“跟随”(查看:F:\laragon\www\tweety\resources\views_friends-list.blade.php)”

我正在使用 Laravel V.6

我正在尝试获取一个从 User.php 获取关注者列表的类,但刀片模板无法读取它

刀片文件包含以下代码

<h3 class="font-bold text-xl mb-4">Following</h3>

<ul>
    @foreach(auth()->user()->follows as $user)
    <li class="mb-4">
        <div>
                <a class="flex items-center text-sm" href="{{route('profile',$tweet->$user)}}">
                <img
                    src="{{$user->avatar}}"
                    alt=""
                    class="rounded-full mr-2"
                >
                    {{$user->name}}

                </a>
           </div>
    </li>
    @endforeach
</ul>

user.php 中的函数是

 public function follows()
{
    return $this->belongsToMany(User::class, 'follows', 'user_id', 'following_user_id');
}

【问题讨论】:

  • 错误说你没有设置属性如下。 user.php 中的 follow() 方法在哪里?
  • ErrorException Trying to get property 'follows' of non-object 它可能会尝试从null 获取follows 你确定你已经通过身份验证了吗?
  • 对不起,我提到了一个错误的方法,我提到了我称之为正确的方法
  • 没有我没有通过身份验证
  • 那么Auth::user() 将返回null,因为这是为了返回经过身份验证的用户

标签: laravel laravel-blade


【解决方案1】:

当我从模板中删除 $tweet 时,我发现它可以正常工作,并且可以这样工作

非常感谢你们的帮助

【讨论】:

    猜你喜欢
    • 2020-07-28
    • 2021-11-15
    • 1970-01-01
    • 1970-01-01
    • 2020-02-04
    • 2016-05-26
    • 2021-10-19
    • 1970-01-01
    • 2021-12-22
    相关资源
    最近更新 更多