ajax - laravel如何将分页结果发送到ajax请求中
问题描述
在 ajax 调用中的 laravel make view 中,页面未正确加载。它像下图一样加载。
解决方案
你可以试试雄辩的资源
使用以下命令生成资源类:
php artisan make:resource User
注意:使用您自己的型号名称代替
User
.然后使用以下命令创建资源集合类:
php artisan make:resource UserCollection
或者
php artisan make:resource Users --collection
像这样返回对 ajax 的响应:
public function getUsers() { $users = User::paginate(); /** * this will convert your collection into array and * also sends the additional pagination information. */ return new UserCollection($users); // or // return new Users($users); }
此外,您可以像这样管理/转换资源中的响应toArray()
:
class User extends JsonResource
{
/**
* Transform the resource into an array.
*
* @param \Illuminate\Http\Request
* @return array
*/
public function toArray($request)
{
return [
'id' => $this->id,
'name' => $this->name,
'email' => $this->email,
'created_at' => $this->created_at,
'updated_at' => $this->updated_at,
];
}
}
推荐阅读
- virtual-machine - 如何在virtualbox中将包加载到openwrt
- c# - 将二进制文件字节数组发送到 web api 方法
- spring - 到 JSON 的 SolrDocumentList 忽略了 solrj 响应中的 numFound 属性
- javascript - 为什么嵌套对象不通过引用传递?
- python - 为什么python函数使用相同的参数返回不同的结果?
- c - Linux下抓包时IP地址相同(C程序)
- java - 如何从 Eclipse 或其他方式调试 .bsh 文件
- mongodb - 如何从 mongodb 迁移到 gcp 数据存储
- macos - Safari 浏览器中的动作链失败
- .htaccess - 使用子文件夹重定向域删除斜线