在Laravel视图中未定义的偏移量:0
我说之前有问题
期望参数1中的数组,但给出了对象
,然后通过->toArray
进行修复。然后出现了这个问题
public function index()
{
$products = Product::paginate(3)->toArray();
return view ('inventory.layout', [
'products' => $products
]);
}
这是在查看文件中显示我的产品
我试图用['data']
字符串替换0键,但是没用。
@if (isset($products))
@if ($arrkeys = array_keys($products[0]))
@foreach ($arrkeys as $key)
<th>{{$key}}</th>
@endforeach
@endif
@endif
答案 0 :(得分:0)
如果您需要显示分页,请使用以下代码
public function index()
{
$products = Product::paginate(3);
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))
@foreach ($products as $key)
<th>{{$key}}</th>
@endforeach
//for pagination
<div>{{$products->links()}}</div>
@endif
答案 1 :(得分:0)
如果要显示数组的键,请尝试以下操作:
@if (isset($products))
@foreach ($products as $key => $value)
<th>{{$key}}</th>
@endforeach
@endif
但是如果要显示值:
@if (isset($products))
@foreach ($products as $key => $value)
<th>{{$value}}</th>
@endforeach
@endif