如何在后端的Laravel中从数据库中获取数据的分页

时间:2019-10-09 10:49:41

标签: php laravel

我的数据库中有6个不同的表,我现在从数据库中的每个表中获取一些值,所以我必须在后端进行分页,即我想根据每个帖子的页码和页数进行显示,就像我传递参数一样像page_number并计数,我的api将返回该page_number上确切的帖子数。该计数用于帖子的累积计数,即包括所有6个类别帖子名称,分别为$ u_article,$ u_meetup,$ u_job,$ u_question,$ u_event,$ u_education。

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();

但是它为每个单独类别而不是累积类别提供每页的帖子

1 个答案:

答案 0 :(得分:0)

使用Laravel DB对象或雄辩模型引用Laravel分页。 https://laravel.com/docs/5.8/pagination