i have a question. How can i return view with variable who is null if there is no record in DB. i know return $variable ?? null; but i need this in a view.
public function shop_items($shelf)
{
$shop_shelf = $this->getTreeItems(0);
$shop_items = DB::table("shop_items")->where('shelf_id', $shelf)->get();
return view('shop',compact('shop_items','shop_shelf'));
}
if there is record i get the valeu of $shop_items but if there is no such record got Undefined offset: 0
edite: add shop.blade
@if(isset($shop_shelf))
@foreach($shop_shelf as $ss)
<ul>
<li>{{$ss["title"]}}
@if(is_array($ss["children"]))
<ul>
@foreach($ss["children"] as $sc)
<li>
<a href="/shop/shelf/{{$sc["id"]}}">
{{$sc["title"]}}
</a>
</li>
@endforeach
</ul>
@endif
</li>
</ul>
@endforeach
could not post all code so i put an image of shop.blade
You may use
@forelse($shop_shelf as $ss)
<ul>
<li>{{$ss["title"]}}
@if(is_array($ss["children"]))
<ul>
@foreach($ss["children"] as $sc)
<li>
<a href="/shop/shelf/{{$sc["id"]}}">
{{$sc["title"]}}
</a>
</li>
@endforeach
</ul>
@endif
</li>
</ul>
@empty
<p>No data to display</p>
@endforelse
PS : apply the @forelse loop structure wherever you have an
array
in your blade
See docs