我想把 paginate(10)
以便以后添加分页,但当我添加时,它给我这个错误。
Undefined property: Illuminate\Pagination\LengthAwarePaginator::$product
这是我的控制器,我想在这里添加 pagination(10)
:
public function index()
{
$user_id = auth()->user()->id;
$user = User::find($user_id);
return view('your')->with('product', $user->product);
}
我已经试过了。
public function index()
{
$user_id = auth()->user()->id;
$user = User::find($user_id)->paginate(10);
return view('your')->with('product', $user->product);
}
我怎样才能避免未定义属性的错误?
实际上 find
函数只用于获取1个结果,而你却在该结果上应用了分页,这导致了一个错误。
你可能希望产品数据被分页,为此,你可以这样做。
public function index()
{
$user_id = auth()->user()->id;
$user = User::find($user_id)->product()->paginate(10);
return view('your')->with('product', $user);
}