-1

Hello I have this variable in php with data but in some pages I don't have it defined so I need to replace it with some data but my code doesn't work.

$item->restaurant->name

Try with:

$item->item_category->name = "Mi content replace";

But it does not work. What am I doing wrong? Thanks

  • 1
    pls include all _relevant_ code. we can't tell from the little you put here. Can you do a `print_r($item)` and show inyour question what the output is? – Kinglish Jun 13 '21 at 23:20
  • _"it does not work"_ is not a problem description. Explain what is happening and what you expect to happen, and Include here the complete text of any error messages you see. – Tangentially Perpendicular Jun 13 '21 at 23:25

1 Answers1

2

Since PHP 7.0 you can use the null coalescing operator ??. Which tries to fetch your $variable or else default to something else.

$nameOrDefault = $item->restaurant->name ?? "Mi content replace";

Or in a blade context

<p>
    {{ $item->restaurant->name ?? "Mi content replace" }}
</p>
mrhn
  • 17,961
  • 4
  • 27
  • 46