有没有办法在视图中使用模型的静态函数?我想要做的是在视图中有展位 ID 时获取展位名称。
{{booth::find($voter->boothId)->name}}
Run Code Online (Sandbox Code Playgroud)
是的,你可以,但类名必须是全名,例如 App\booth
{{App\booth::find($voter->boothId)->name}}
Run Code Online (Sandbox Code Playgroud)
或者你可以在 app.config 中添加类别名
'aliases' => [
....
'booth' => App\boot::class \\ your class namespace
],
Run Code Online (Sandbox Code Playgroud)
并在 .blade 中使用
{{booth::find($voter->boothId)->name}}
Run Code Online (Sandbox Code Playgroud)
但是为了良好的实践,您必须检查模型的booth::find($voter->boothId)null 或实例例如boot
@php($booth = booth::find($voter->boothId))
{{$booth ? $booth->name : 'some content'}}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
681 次 |
| 最近记录: |