【发布时间】: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