微信扫一扫打赏支持

laravel的post请求分页数据

laravel的post请求分页数据

一、总结

一句话总结:

可以直接paginate向前端返回分页数据,ajax请求的时候我们写个方法取需要的分页数据

开始用paginate

//5、获取用户的博客信息
$blogs=Blog::where('b_u_id',$login_u_id)->where('b_is_delete',0)->paginate(10);
$blogs=$blogs->toArray();
//dd($blogs);

if(!isset($blogs['next_page_url'])) $blogs['next_page_url']='';
if(!isset($blogs['prev_page_url'])) $blogs['prev_page_url']='';

$blogs_json=\GuzzleHttp\json_encode($blogs);

后面用自己

//获取视频对应分页博客的ajax
public function get_paginate_blogs(Request $request){
    $back_data=[];
    $back_data['valid']=0;
    $back_data['message']='';

    //获取用户id
    Auth::guard('user')->user();
    $login_u_id = Auth::guard('user')->user()->id;

    $now_page=intval($request->input('now_page'));
    $per_page_num=intval($request->input('per_page_num'));

    //偏移量
    $offset=($now_page-1)*$per_page_num;
    if($offset<0) $offset=0;

    //5、获取用户的博客信息
    $blogs=Blog::where('b_u_id',$login_u_id)->where('b_is_delete',0)->offset($offset)->limit($per_page_num)->get();
    $blogs=$blogs->toArray();

    $blogs_json=\GuzzleHttp\json_encode($blogs);

    $back_data['valid']=1;
    $back_data['message']='获取博客分页数据成功!';
    $back_data['blogs_json']=$blogs;
    return $back_data;
}

 

 

 

 

二、laravel的post请求分页数据

 

操作真的非常简单

1、分页原理

其实分页就是查两次表:

1、查总数目

select count(*) as aggregate from `blogs` where `b_u_id` = 11 and `b_is_delete` = 0

 

2、查我们需要的数据,偏移offset,我们取的limit

select * from `blogs` where `b_u_id` = 11 and `b_is_delete` = 0 limit 10 offset 0

 

 

 

2、代码

1、可以直接paginate向前端返回分页数据,ajax请求的时候我们写个方法取需要的分页数据

开始用paginate

//5、获取用户的博客信息
$blogs=Blog::where('b_u_id',$login_u_id)->where('b_is_delete',0)->paginate(10);
$blogs=$blogs->toArray();
//dd($blogs);

if(!isset($blogs['next_page_url'])) $blogs['next_page_url']='';
if(!isset($blogs['prev_page_url'])) $blogs['prev_page_url']='';

$blogs_json=\GuzzleHttp\json_encode($blogs);

后面用自己

//获取视频对应分页博客的ajax
public function get_paginate_blogs(Request $request){
    $back_data=[];
    $back_data['valid']=0;
    $back_data['message']='';

    //获取用户id
    Auth::guard('user')->user();
    $login_u_id = Auth::guard('user')->user()->id;

    $now_page=intval($request->input('now_page'));
    $per_page_num=intval($request->input('per_page_num'));

    //偏移量
    $offset=($now_page-1)*$per_page_num;
    if($offset<0) $offset=0;

    //5、获取用户的博客信息
    $blogs=Blog::where('b_u_id',$login_u_id)->where('b_is_delete',0)->offset($offset)->limit($per_page_num)->get();
    $blogs=$blogs->toArray();

    $blogs_json=\GuzzleHttp\json_encode($blogs);

    $back_data['valid']=1;
    $back_data['message']='获取博客分页数据成功!';
    $back_data['blogs_json']=$blogs;
    return $back_data;
}

 


2、我们也可以直接就从我们写的分页方法中取数据

这个也非常简单,就不贴代码了

 
posted @ 2021-09-16 20:42  范仁义  阅读(327)  评论(0编辑  收藏  举报