在拉拉维尔"Undefined offset: 0"



在拉拉维尔视图中 未定义的偏移量:0

我之前有问题说

期望参数 1 中有数组,但给定对象

并通过->toArray修复它。然后出现了这个问题

public function index()
{
$products = Product::paginate(3)->toArray();
return view ('inventory.layout', [
'products' => $products
]);
}

这是为了在查看文件中显示我的产品 我试图用字符串替换 0 键['data']但没用。

@if (isset($products)) 
@if ($arrkeys = array_keys($products[0])) 
@foreach ($arrkeys as $key)  
<th>{{$key}}</th>
@endforeach
@endif 
@endif

如果需要显示分页,请使用这样的代码

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

如果要显示数组的,请尝试以下操作:

@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

最新更新