Laravel - How to try / catch as a blade?

I would like to create a way to compile in a try / catch view. How can I do this in Laravel?

Example:

@try
<div class="laravel test">
    {{ $user->name }}
</div>
@catch(Exception $e)
    {{ $e->getMessage() }}
@endtry
+4
source share
2 answers

Your view should not have blocks try/ catch. A view is that: a representation of some data. This means that you should not execute any logic (for example, exception handling). This belongs to the controller as soon as you select data from the models.

If you just need to display the default, if the variable is undefined, you can use the keyword or:

{{ $user->name or 'Name not set' }}
+14
source

, - , try..catch . , . .

, :

PHP , <?php ?> @php @endphp. try..catch PHP.

@php
    try {
        // Try something
    } catch (\Exception $e) {
        // Do something exceptional
    }
@endphp
+1

All Articles