Laravel雄辩的关系在做任何事情时都不是对象,但dd() [英] Laravel Eloquent relationship not an object when doing anything but dd()
问题描述
我有一个看似愚蠢和疯狂的情况.我有用户的活动日志,我的用户模型具有这种关系:
I have a seemingly stupid and crazy situation. I have activity logs for users, my user model has this relationship in it:
public function activityLogs()
{
return $this->hasMany(Log::class);
}
相当简单,当我检查单个用户对象的所有日志时,它工作正常.因此,在一页上,我只希望记录最后的日志,我需要其中的created_at
日期.加载用户时,我会运行
Pretty straight forward and when I check a single user object for all logs it works fine. So on one page I only want the last log recorded, I need the created_at
date from it. When loading the users I run
$users = User::with(
[
'activityLogs' => function ($query) {
$query->orderBy('created_at', 'desc')
->limit(1);
}
]
)
,这将按预期返回最后一个日志.如果我想获取日期(如果我运行)
and this returns the last log as expected. When I want to get the date if I run
dd($users->first()->activityLogs()->first()->created_at->format("d/m/Y"));
我得到一个带有预期日期的字符串输出.但是,当我尝试对它进行任何其他处理时,例如将其放入变量中或将其回显,则只会收到一个错误,提示ActivityLogs()-> first()不是对象.我认为(在foreach ($users as $user)
循环内)的代码是
I get a string output with the date as expected. However, when I try to do anything else with it, such as putting it into a variable or echoing it out I just get an error that activityLogs()->first() is not an object. The code in my view (inside a foreach ($users as $user)
loop) is
{{ $user->activityLogs()->first()->created_at }}
这只会给我错误
ErrorException(E_ERROR) 尝试获取非对象的属性(视图: ROOT_PATH /resources/views/acp/partials/profiles.blade.php)(视图: ROOT_PATH /resources/views/acp/partials/profiles.blade.php)
ErrorException (E_ERROR) Trying to get property of non-object (View: ROOT_PATH/resources/views/acp/partials/profiles.blade.php) (View: ROOT_PATH/resources/views/acp/partials/profiles.blade.php)
我尝试将activityLogs
作为集合和hasMany对象进行访问.我也尝试过用json_decode
转换回生成的JSON字符串,但它仍然抱怨不是对象.
I've tried accessing activityLogs
as both a collection and hasMany object. I've also tried converting the resultant JSON string back with json_decode
but it still complains about not being an object.
为什么?为什么在使用dd
时我可以得到很好的值,但是当我尝试其他任何方法时,它突然变成了JSON字符串?我尝试使用谷歌搜索,但是无论我尝试使用哪种单词组合,它都提出了有关如何将Eloquent对象转换为JSON的问题和指南,与我想要的相反.
Why? Why can I get the value perfectly fine when using dd
but when I try anything else it's suddenly a JSON string? I've tried googling this but no matter what combination of words I try it just comes up with questions and guides on how to convert an Eloquent object into JSON, the opposite of what I want.
推荐答案
您需要检查以确保用户进行了最后一次活动.如果没有任何日志,first()
将返回null.您应该检查循环中是否存在.
You need to check to make sure the user has a last activity. first()
will return null if there aren't any logs. You should check for existence in your loop.
@foreach($user as $user)
{{$user->activityLogs->count() > 0 ? $user->activityLogs->first()->created_at : 'User Has No Activity!'}}
@endforeach
或使用刀片服务器的@if/@else
@foreach($user as $user)
@if($user->activityLogs->count() > 0)
{{$user->activityLogs->first()->created_at}}
@else
'User Has No Activity!'
@endif
@endforeach
这篇关于Laravel雄辩的关系在做任何事情时都不是对象,但dd()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!