0

i'm debugging a laravel project, and there are many instances where the developer simply dropped double mustaches into the blade templates referencing object properties, without first checking if those relations exist.

example

{{$category->name}}

<small>{{$category->parent->name}}</small>

Is there a built in way in laravel to just silently fail if for example $category is not set, or $category->parent is not set, without resorting to something along these lines...

@if (isset($category))
   {{$category->name}}
@endif

@if (isset($main_category->parent))
    @if($main_category->parent != null)
        <small>{{$main_category->parent->name}}</small>
    @endif
@endif
Andrelope
  • 103
  • 1
  • 11
  • Can you share the code where you get the value for the parent property ? – Vagabond May 10 '19 at 14:05
  • I don't know Blade that well anymore, but can you try: `{{$category->name ?? ''}}` and see if that works? – M. Eriksson May 10 '19 at 14:05
  • 1
    https://stackoverflow.com/questions/18023480/how-to-echo-a-default-value-if-value-not-set-blade – bassxzero May 10 '19 at 14:06
  • 2
    Possible duplicate of [How to echo a default value if value not set blade](https://stackoverflow.com/questions/18023480/how-to-echo-a-default-value-if-value-not-set-blade) – M. Eriksson May 10 '19 at 14:08
  • You can do it as easily as `if($main_category->parent)`, which will check for the existing relationship. It's a bit harder for `$category`, since the variable may or may not exist, so the first option is best – aynber May 10 '19 at 14:10
  • 2
    Are you sure you want to go this way? For the second example, this would mean printing an empty `` tag - I would vote to fix the errors instead of hiding them – Nico Haase May 10 '19 at 14:12
  • I am leaning in this direction. in certain cases i may want to simply suppress the error. I am thinking this is going to be long hard trek through the mud to get this cleaned up. Thank you for your input. – Andrelope May 10 '19 at 15:49
  • Suppressing errors is never a good solution... You need to handle them. – Lulceltech May 10 '19 at 15:57

2 Answers2

0

There is better way to hide error you get,

{{ @$category }}

@ will suppress error for undefined or unset variables.

PHP supports one error control operator: the at sign (@). When prepended to an expression in PHP, any error messages that might be generated by that expression will be ignored.

Here is official php documentation for error control operator.

Rahul
  • 18,271
  • 7
  • 41
  • 60
  • 1
    in terms of what i asked this is the cleanest looking solution i think... unless there's a way to make it do that by default in the models or something. thank you. – Andrelope May 10 '19 at 15:50
0

There is a better way.

{{ $category->name ?? '' }}
// or
{{ $category->name or '' }} // for older version of laravel
  • this works fine if you are using a recent version of php, if you are using an older one, you will need to use the full syntax ( condition ? if-true : if-false) – Andrelope Oct 10 '19 at 14:46