Laravel - How to make a Try/catch in the view?

Asked

Viewed 361 times

0

Is there any way to make some try/catch in a view of Laravel 4? How could I implement in the syntax of blade?

I’d like something like:

@try
<div class="laravel test">
    {{ $usuario->nome }}
</div>
@catch(Exception $e)
    {{ $e->getMessage() }}
@endtry

1 answer

2

It’s not the right thing to Try/catch in the view. The idea is to always return something to it so you don’t have to do this validation.

You can check if the variable has value, if it has its value, if not, no.

@if $usuario->nome
    {{$usuario->nome}}
@endif

or else

{{$usuario->nome or 'Nome não informado'}}
  • They told me that at SOEN. But, as I also said there, I say here: I know it’s wrong, but I would like to know this because of the curiosity to know how to do it in the compiler of Blade

  • Got it. You can write the normal php there. Blade doesn’t have @Try, but if you play php there it will work normally.

  • Buddy, actually I wanted something I can do with Blade Extension

Browser other questions tagged

You are not signed in. Login or sign up in order to post.