Laravel Blade 显示数据
Laravel Blade Displaying Data
我对 laravel(版本 7){{ }} 的语法有一个奇怪的问题,我在其中使用 PHP 这样的语法:
<?php echo $thread->lastReply->user->name; ?>
我显示了我的用户,但使用 blade 语法:
{{$thread->lastReply->user->name}}
我收到这个错误:
ErrorException
Trying to get property 'user' of non-object
问题可能来自哪里?
编辑:
thread中的lastReplay方法是这样的:
public function getLastReplyAttribute()
{
$lastReplay = $this->replies->last();
dd($lastReplay);
return $lastReplay;
}
和转储:
\Models\Reply {#3012 ▼
#table: "ww_replies"
#connection: "mysql"
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:10 [▼
"id" => "42"
"thread_id" => "45"
"user_id" => "1"
"title" => null
"body" => "Quia minima velit illo aut vitae eum et. Fuga consequatur atque earum fuga ad aut molestiae. Expedita ratione perspiciatis laborum hic quis unde. Numquam ipsam ▶"
"slug" => null
"ip" => null
"created_at" => "2020-11-23 12:24:19"
"updated_at" => "2020-11-23 12:24:19"
"deleted_at" => null
]
#original: array:10 [▶]
#changes: []
#casts: []
#classCastCache: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: []
#touches: []
+timestamps: true
#hidden: []
#visible: []
#fillable: []
#guarded: array:1 [▶]
}
您的查询返回的是数组还是对象?如果将其转储出来,您可能会发现它是一个数组,而您只需要一个数组访问 ([]) 而不是对象访问 (->)。
这意味着 $thread->lastReply
不是对象,或者通常是 null
。处理你可以使用条件示例:
{{$thread->lastReply ? $thread->lastReply->user->name : 'no last reply user'}}
或
{{$thread->lastReply->user->name ?? 'no last reply user'}}
你的问题来自 [=11th=]。可能有些用户没有 lastReply
?
尝试以下操作:
{{ optional($thread->lastReply, function ($lastReply)
{
return $lastReply->user->name;
});
}}
ErrorException
Trying to get property 'user' of non-object
此错误意味着 lastReply 返回 null 或其数组
{{$thread->lastReply->user->name}}
在此语法中,您需要在添加关系之前检查一些数据
如果 lastReply 正在返回数据则转储
{{dd($thread->lastReply)}}
用户应该存在于模型中并且在relationship.Also中有正确的外键它不应该是空的,你也可以通过下面的代码检查
{{dd($thread->lastReply->user)}}
如果两者都转储返回数据,则表示 lastReply 或用户正在返回数组。所以需要使用array[]语法来获取数据。
也可以使用{!! !!}
打印数据转义content
我对 laravel(版本 7){{ }} 的语法有一个奇怪的问题,我在其中使用 PHP 这样的语法:
<?php echo $thread->lastReply->user->name; ?>
我显示了我的用户,但使用 blade 语法:
{{$thread->lastReply->user->name}}
我收到这个错误:
ErrorException
Trying to get property 'user' of non-object
问题可能来自哪里?
编辑: thread中的lastReplay方法是这样的:
public function getLastReplyAttribute()
{
$lastReplay = $this->replies->last();
dd($lastReplay);
return $lastReplay;
}
和转储:
\Models\Reply {#3012 ▼
#table: "ww_replies"
#connection: "mysql"
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:10 [▼
"id" => "42"
"thread_id" => "45"
"user_id" => "1"
"title" => null
"body" => "Quia minima velit illo aut vitae eum et. Fuga consequatur atque earum fuga ad aut molestiae. Expedita ratione perspiciatis laborum hic quis unde. Numquam ipsam ▶"
"slug" => null
"ip" => null
"created_at" => "2020-11-23 12:24:19"
"updated_at" => "2020-11-23 12:24:19"
"deleted_at" => null
]
#original: array:10 [▶]
#changes: []
#casts: []
#classCastCache: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: []
#touches: []
+timestamps: true
#hidden: []
#visible: []
#fillable: []
#guarded: array:1 [▶]
}
您的查询返回的是数组还是对象?如果将其转储出来,您可能会发现它是一个数组,而您只需要一个数组访问 ([]) 而不是对象访问 (->)。
这意味着 $thread->lastReply
不是对象,或者通常是 null
。处理你可以使用条件示例:
{{$thread->lastReply ? $thread->lastReply->user->name : 'no last reply user'}}
或
{{$thread->lastReply->user->name ?? 'no last reply user'}}
你的问题来自 [=11th=]。可能有些用户没有 lastReply
?
尝试以下操作:
{{ optional($thread->lastReply, function ($lastReply)
{
return $lastReply->user->name;
});
}}
ErrorException
Trying to get property 'user' of non-object
此错误意味着 lastReply 返回 null 或其数组
{{$thread->lastReply->user->name}}
在此语法中,您需要在添加关系之前检查一些数据
如果 lastReply 正在返回数据则转储
{{dd($thread->lastReply)}}
用户应该存在于模型中并且在relationship.Also中有正确的外键它不应该是空的,你也可以通过下面的代码检查
{{dd($thread->lastReply->user)}}
如果两者都转储返回数据,则表示 lastReply 或用户正在返回数组。所以需要使用array[]语法来获取数据。
也可以使用{!! !!}
打印数据转义content