Laravel 与 find 一起工作正常,但在添加分页后它不工作

Laravel working fine with find but it is not working after adding paginate

Table 在没有 paginate(5) 函数的情况下加载正常,但是当我添加分页函数时它显示错误。

public function index(Request $request)
{
    $userid = $request->user()->assets()->get(['id']);
    $arr['love'] = Love::find($userid)->paginate(5);
    return view('admin.love.index')->with($arr);
}

错误:

Whoops, looks like something went wrong.
(1/1) BadMethodCallException

Method paginate does not exist.
in Macroable.php line 74
at Collection->__call('paginate', array(5))in AssetsController.php line 26

当您使用 find 时,它会检索单个记录。您不能在单个记录上调用分页。您需要在模型构建器上调用它。

Love::where('user_id', $user->id)->paginate()

find()方法只有return一个结果。 paginate 不适用于 find 方法。

你可以这样做

public function index(Request $request)
{
     $userid = $request->user()->assets()->get(['id']);
     $arr['love'] = Love::whereIn($'user_id', $user->id)->toArray()->paginate(5);
     return view('admin.love.index')->with($arr);
}

通过使用 whereIn 而不是 where 解决了答案。

public function index(Request $request)
{
     $userid = $request->user()->assets()->get(['id']);
     $arr['love'] = Love::whereIn('user_id', $userid)->paginate(5);
     return view('admin.love.index')->with($arr);
}