【发布时间】:2021-03-08 06:03:46
【问题描述】:
我对 laravel (version 7) {{ }} 的语法有一个奇怪的问题,我使用这样的 PHP 语法:
<?php echo $thread->lastReply->user->name; ?>
我让我的用户显示,但使用刀片语法:
{{$thread->lastReply->user->name}}
我收到了这个错误:
错误异常
试图获取非对象的属性“用户”
问题可能出在哪里?
编辑: 线程中的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作为数据传递给刀片视图? -
我会使用
{{ dump($thread->lastReply) }}看看那里发生了什么。我假设您处于循环中,并且您的迭代之一没有显示 -
是的,它在循环内
标签: php laravel laravel-blade