如何使用返回为 json laravel 进行分页

raf*_*tio 5 php laravel laravel-pagination

我想创建一个返回为 json 的分页,但出现如下错误

Macroable.php 第 74 行中的 ErrorException:方法链接不存在。

这是我的控制器代码

 public function getcustomer($id){

    $customer = Customer::find($id)->paginate(5);

    return response()->json([$customer], 200);
}
Run Code Online (Sandbox Code Playgroud)

这里是我的刀片代码

{{$customer->links('vendor.pagination.pagination')}}
Run Code Online (Sandbox Code Playgroud)

如何使用 json response() 创建分页?

Ale*_*nin 3

Laravel 分页器结果类实现了Illuminate\Contracts\Support\JsonableInterface 契约并公开了该toJson方法,因此可以非常轻松地将分页结果转换为 JSON。

https://laravel.com/docs/5.3/pagination#converting-results-to-json

如果您想建立链接,您应该手动进行。或者您应该照常返回分页,并使用render()方法来构建链接并创建分页对象的副本,以将其转换为 JSON toJson()