In Laravel view Undefined offset: 0
I got problem before says
expecting array in parameter 1 but object given
and fix it through ->toArray
. And then appeared this problem
public function index()
{
$products = Product::paginate(3)->toArray();
return view ('inventory.layout', [
'products' => $products
]);
}
And this is to show my products in view file
I've tried to replace the 0 key with ['data']
string but useless.
@if (isset($products))
@if ($arrkeys = array_keys($products[0]))
@foreach ($arrkeys as $key)
<th>{{$key}}</th>
@endforeach
@endif
@endif