我想创建一种在 View 中编译的方法,即 try/catch。
我怎样才能在 Laravel 中做到这一点?
例子:
@try
<div class="laravel test">
{{ $user->name }}
</div>
@catch(Exception $e)
{{ $e->getMessage() }}
@endtry
最佳答案
你不应该有 try
/catch
阻止您的视线。 View 正是这样:一些数据的表示。这意味着您不应该执行任何逻辑(例如异常处理)。一旦你从模型中获取数据,它就属于 Controller 。
如果您只想在 undefined variable 的情况下显示默认值,您可以使用标准 PHP null coalescing operator显示默认值:
{{ $user->name ?? 'Name not set' }}
关于Laravel - 如何在 Blade View 中尝试/捕获?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31411166/