Php 如何在后端对laravel中从数据库获取的数据进行分页
我的数据库中有6个不同的表,我从数据库中的每个表中获取一些值,现在我必须在后端进行分页,即,我想根据页码和每篇文章的页数进行显示,就像如果我传递参数,如page_number和count,我的api将返回该页码上的确切文章数。该计数是针对帖子的累积计数,即包括所有6类帖子名称$u_文章、$u会议、$u工作、$u问题、$u活动、$u教育Php 如何在后端对laravel中从数据库获取的数据进行分页,php,laravel,Php,Laravel,我的数据库中有6个不同的表,我从数据库中的每个表中获取一些值,现在我必须在后端进行分页,即,我想根据页码和每篇文章的页数进行显示,就像如果我传递参数,如page_number和count,我的api将返回该页码上的确切文章数。该计数是针对帖子的累积计数,即包括所有6类帖子名称$u_文章、$u会议、$u工作、$u问题、$u活动、$u教育 public function wsUserActivity(){ $request = Input::all(); try {
public function wsUserActivity(){
$request = Input::all();
try {
$user_id = $request['user_id'];
$no=isset($request['page_number'])?$request['page_number']:0;
$nos=isset($request['count'])?$request['count']:10;
$skp=$no*$nos;
$array_json_return = array('status' => '1','msg' => 'Success');
$u_article = array();
$u_article = DB::table('mst_article as article')
->select('id','title', DB::raw('DATE_FORMAT(created_at, "%d %b %Y") as created_at'), DB::raw('DATE_FORMAT(updated_at, "%d %b %Y") as updated_at'), 'imported', 'import_url', 'cover_type', 'profile_image')
->selectRaw('SUBSTRING(`description`, 1, 200) as `description`')
->where('user_id_fk',$user_id)
->where('status', '=', '1')
->get();
if (count($u_article)>0){
foreach ($u_article as $key => $value){
$u_article[$key]->type = 'article';
if (!empty($value->profile_image)){
$u_article[$key]->profile_image_url = config("feature_image_path").'article_image/thumb/'.$value->profile_image;
}
$u_article[$key]->post_url = url('/') . '/view-article' . '/' . $value->id;
}
}
foreach ($u_article as $object){
$u_articles[] = (array) $object;
}
$u_meetup = array();
$u_meetup = DB::table('mst_meetup as meetup')
->select('id','title', DB::raw('DATE_FORMAT(created_at, "%d %b %Y") as created_at'), DB::raw('DATE_FORMAT(updated_at, "%d %b %Y") as updated_at'), DB::raw('DATE_FORMAT(start_date, "%d %b %Y") as start_date'), 'imported', 'import_url', 'cover_type', 'profile_image', 'location')
->selectRaw('SUBSTRING(`description`, 1, 200) as `description`')
->where('user_id_fk',$user_id)
->where('status', '=', '1')
->get();
if (count($u_meetup)>0){
foreach ($u_meetup as $key => $value){
$u_meetup[$key]->type = 'meetup';
if (!empty($value->profile_image)){
$u_meetup[$key]->profile_image_url = config("feature_image_path").'meetup_image/thumb/'.$value->profile_image;
}
$u_meetup[$key]->post_url = url('/') . '/view-meetup' . '/' . $value->id;
}
}
foreach ($u_meetup as $object){
$u_meetups[] = (array) $object;
}
$u_question = array();
$u_question = DB::table('mst_question as question')
->select('id','title', DB::raw('DATE_FORMAT(created_at, "%d %b %Y") as created_at'), DB::raw('DATE_FORMAT(updated_at, "%d %b %Y") as updated_at'), 'imported', 'import_url', 'cover_type', 'profile_image')
->selectRaw('SUBSTRING(`description`, 1, 200) as `description`')
->where('user_id_fk',$user_id)
->where('status', '=', '1')
->get();
if (count($u_question)>0){
foreach ($u_question as $key => $value){
$u_question[$key]->type = 'question';
if (!empty($value->profile_image)){
$u_question[$key]->profile_image_url = config("feature_pic_url").'question_image/thumb/'.$value->profile_image;
}
$u_question[$key]->post_url = url('/') . '/view-question' . '/' . $value->id;
}
}
foreach ($u_question as $object){
$u_questions[] = (array) $object;
}
$u_job = array();
$u_job = DB::table('mst_job as job')
->select('id','title', DB::raw('DATE_FORMAT(created_at, "%d %b %Y") as created_at'), DB::raw('DATE_FORMAT(updated_at, "%d %b %Y") as updated_at'), 'imported', 'import_url', 'cover_type', 'profile_image', 'job_location', 'cmp_name')
->selectRaw('SUBSTRING(`description`, 1, 200) as `description`')
->where('user_id_fk',$user_id)
->where('status', '=', '1')
->get();
if (count($u_job)>0){
foreach ($u_job as $key => $value){
$u_job[$key]->type = 'job';
if (!empty($value->profile_image)){
$u_job[$key]->profile_image_url = config("feature_pic_url").'job_image/thumb/'.$value->profile_image;
}
$u_job[$key]->post_url = url('/') . '/view-job' . '/' . $value->id;
}
}
foreach ($u_job as $object){
$u_jobs[] = (array) $object;
}
$u_education = array();
$u_education = DB::table('mst_education as education')
->select('id','title', DB::raw('DATE_FORMAT(created_at, "%d %b %Y") as created_at'), DB::raw('DATE_FORMAT(updated_at, "%d %b %Y") as updated_at'), 'imported', 'import_url', 'cover_type', 'profile_image')
->selectRaw('SUBSTRING(`description`, 1, 200) as `description`')
->where('user_id_fk',$user_id)
->where('status', '=', '1')
->get();
if (count($u_education)>0){
foreach ($u_education as $key => $value){
$u_education[$key]->type = 'education';
if (!empty($value->profile_image)){
$u_education[$key]->profile_image_url = config("feature_pic_url").'education_image/thumb/'.$value->profile_image;
}
$u_education[$key]->post_url = url('/') . '/view-education' . '/' . $value->id;
}
}
foreach ($u_education as $object){
$u_educations[] = (array) $object;
}
$u_event = array();
$u_event= DB::table('mst_event as event')
->select('id','title', DB::raw('DATE_FORMAT(created_at, "%d %b %Y") as created_at'), DB::raw('DATE_FORMAT(updated_at, "%d %b %Y") as updated_at'), DB::raw('DATE_FORMAT(start_date, "%d %b %Y") as start_date'), 'imported', 'import_url', 'cover_type', 'profile_image', 'location')
->selectRaw('SUBSTRING(`description`, 1, 200) as `description`')
->where('user_id_fk',$user_id)
->where('status', '=', '1')
->get();
if (count($u_event)>0){
foreach ($u_event as $key => $value){
$u_event[$key]->type = 'event';
if (!empty($value->profile_image)){
$u_event[$key]->profile_image_url = config("feature_pic_url").'event_image/thumb/'.$value->profile_image;
}
$u_event[$key]->post_url = url('/') . '/view-event' . '/' . $value->id;
}
}
foreach ($u_event as $object){
$u_events[] = (array) $object;
}
$u_activity = array();
$u_activity = array_merge($u_articles, $u_meetups, $u_questions, $u_jobs, $u_educations, $u_events);
$array_json_return['u_activity'] = $u_activity;
} catch (\Exception $e) {
$array_json_return = $this->api_default_fail_response(__function__, $e);
}
echo json_encode($array_json_return);
}
我试过这个
$u_article = array();
$u_article = DB::table('mst_article as article')
->select('id','title', DB::raw('DATE_FORMAT(created_at, "%d %b %Y") as created_at'), DB::raw('DATE_FORMAT(updated_at, "%d %b %Y") as updated_at'), 'imported', 'import_url', 'cover_type', 'profile_image')
->selectRaw('SUBSTRING(`description`, 1, 200) as `description`')
->where('user_id_fk',$user_id)
->where('status', '=', '1')
->get();
但它为每个单独的类别提供每页的帖子,而不是累积类别。请使用Laravel DB对象或雄辩的模型参考Laravel分页。
请使用Laravel DB对象或雄辩模型参考Laravel分页。
如果您能将代码减少到再现问题所需的最低限度,这将非常有用。你能解释一下到目前为止你所尝试的方法的问题吗?请参阅。我已尝试使用“$u_article=array()”$u_article=DB::table('mst_article as article')->select('id','title',DB::raw('DATE_格式(创建于,“%d%b%Y”)作为创建于'),DB::raw('DATE_格式(更新于,“%d%b%Y”)作为更新于'),'import_url','cover_type','profile_image')->selectRaw('SUBSTRING(
description
,1200)asdescription
)->where('user_id_fk',$user_id)->where('status','=','1')->skip($skp)->take($nos)->get();'但它需要计算每个类别的数量和页码,而不是累计的数量。很难阅读注释中的代码。请将其编辑到您的问题中。如果您可以将代码减少到重现问题所需的最小值,这将非常有用。你能解释一下到目前为止你所尝试的方法的问题吗?请参阅。我已尝试使用“$u_article=array()”$u_article=DB::table('mst_article as article')->select('id','title',DB::raw('DATE_格式(创建于,“%d%b%Y”)作为创建于'),DB::raw('DATE_格式(更新于,“%d%b%Y”)作为更新于'),'import_url','cover_type','profile_image')->selectRaw('SUBSTRING(description
,1200)asdescription
)->where('user_id_fk',$user_id)->where('status','=','1')->skip($skp)->take($nos)->get();'但它需要计算每个类别的数量和页码,而不是累计的数量。很难阅读注释中的代码。请把它编辑成你的问题。