我试图在我的文章中回应用户的名字而我正在接受 ErrorException: Trying to get property of non-object.我的代码:
楷模
1. News
class News extends Model
{
public function postedBy()
{
return $this->belongsTo('App\User');
}
protected $table = 'news';
protected $fillable = ['newsContent', 'newsTitle', 'postedBy'];
}
2. User
class User extends Model implements AuthenticatableContract,
AuthorizableContract,
CanResetPasswordContract
{
use Authenticatable, Authorizable, CanResetPassword;
protected $table = 'users';
protected $fillable = ['name', 'email', 'password'];
protected $hidden = ['password', 'remember_token'];
}
Run Code Online (Sandbox Code Playgroud)
架构
表 users
表 news
调节器
public function showArticle($slug)
{
$article = News::where('slug', $slug)->firstOrFail();
return view('article', compact('article'));
}
Run Code Online (Sandbox Code Playgroud)
刀
{{ $article->postedBy->name }}
Run Code Online (Sandbox Code Playgroud)
当我尝试删除刀片中的名称时{{ $article->postedBy }}它会输出id,但是当我尝试添加 - >名称时,它会说Trying to get property of non-object但是name我的表中有一个字段和一个User模型.我错过了什么吗?
Jim*_*oto 66
您的查询是返回数组还是对象?如果将其转储出来,您可能会发现它是一个数组,您只需要一个数组访问([])而不是对象访问( - >).
wob*_*ano 23
I got it working by using Jimmy Zoto's answer and adding a second parameter to my belongsTo. Here it is:
First, as suggested by Jimmy Zoto, my code in blade from $article->poster->name to $article->poster['name']. Next is to add a second parameter in my belongsTo, from return $this->belongsTo('App\User'); to return $this->belongsTo('App\User', 'user_id'); in which user_id is my foreign key in the news table.
Thanks for all your help!
Meh*_*tün 19
如果您使用 或 循环(for、foreach等)或关系(one to many、many to many等),这可能意味着其中一个查询正在返回null变量或null关系成员。
例如:在一个表中,您可能希望列出users他们的roles.
<table>
<tr>
<th>Name</th>
<th>Role</th>
</tr>
@foreach ($users as $user)
<tr>
<td>{{ $user->name }}</td>
<td>{{ $user->role->name }}</td>
</tr>
@endforeach
</table>
Run Code Online (Sandbox Code Playgroud)
在上述情况下,即使有一个没有角色的用户,您也可能会收到此错误。此时应更换{{ $user->role->name }}使用{{ !empty($user->role) ? $user->role->name:'' }},这样的:
<table>
<tr>
<th>Name</th>
<th>Role</th>
</tr>
@foreach ($users as $user)
<tr>
<td>{{ $user->name }}</td>
<td>{{ !empty($user->role) ? $user->role->name:'' }}</td>
</tr>
@endforeach
</table>
Run Code Online (Sandbox Code Playgroud)
编辑:您可以使用 Laravel 的optional方法来避免错误(更多信息)。例如:
<table>
<tr>
<th>Name</th>
<th>Role</th>
</tr>
@foreach ($users as $user)
<tr>
<td>{{ $user->name }}</td>
<td>{{ optional($user->role)->name }}</td>
</tr>
@endforeach
</table>
Run Code Online (Sandbox Code Playgroud)
如果您使用的是 PHP 8,则可以使用null safe operator:
<table>
<tr>
<th>Name</th>
<th>Role</th>
</tr>
@foreach ($users as $user)
<tr>
<td>{{ $user?->name }}</td>
<td>{{ $user?->role?->name }}</td>
</tr>
@endforeach
</table>
Run Code Online (Sandbox Code Playgroud)
碰巧过了一段时间我们需要运行
'php artisan passport:install --force
Run Code Online (Sandbox Code Playgroud)
再次生成一个密钥,这解决了我的问题,
发生这种情况的原因(解释)
假设我们有 2 个表users和subscription。
1 个用户有 1 个订阅
在用户模型中,我们有
public function subscription()
{
return $this->hasOne('App\Subscription','user_id');
}
Run Code Online (Sandbox Code Playgroud)
我们可以按如下方式访问订阅详细信息
$users = User:all();
foreach($users as $user){
echo $user->subscription;
}
Run Code Online (Sandbox Code Playgroud)
如果任何用户没有订阅,这可能是一种情况。订阅后我们无法进一步使用箭头功能,如下所示
$user->subscription->abc [this will not work]
$user->subscription['abc'] [this will work]
Run Code Online (Sandbox Code Playgroud)
但如果用户有订阅
$user->subscription->abc [this will work]
Run Code Online (Sandbox Code Playgroud)
注意:尝试设置这样的 if 条件
if($user->subscription){
return $user->subscription->abc;
}
Run Code Online (Sandbox Code Playgroud)