my controller method returns this array, I know data is being returned
return view('bill')->with('itemarray',Menu::where('itemname','Apple Pie')->get());
my view is suppose to act on it like this but the print_r method prints the array but the td of the table has nothing in it and I am not getting an error
@if(isset($itemarray))
<table>
<tr>
<td>{{ $itemarray->pull('itemname') }} <!-- this prints nothing --> </td>
<td> {{ $itemarray->pull('itemprice') }} <!-- this prints nothing --> </td>
</tr>
</table>
<p> {{ print_r($itemarray) }} <!-- this prints ok --> </p>
@endif
The underlying problem here is that get()
always returns a collection. Even if your query only has one or zero results. It's recommended to use first()
if you expect only one result:
return view('bill')->with('item',Menu::where('itemname','Apple Pie')->first());
This means that you will then be dealing with a model in your view, so:
@if(isset($item))
<table>
<tr>
<td>{{ $item->itemname }}</td>
<td> {{ $item->itemprice }}</td>
</tr>
</table>
@endif
I also suggest you change isset()
to !empty()
as this will also check for null
(if no records match)
@if(!empty($item))