如何使用自定义数组添加laravel分页?

时间:2016-03-09 10:50:59

标签: laravel laravel-4 laravel-5.1

我想在自定义laravel查询中添加分页。 这是我的代码。

$jobseekers = Jobseeker::where('employer_id', Auth::employer()->get()->id)->get()->toArray();

$jobs = Job::select('id')->where('employer_id', (Auth::employer()->get()->id))->get()->toArray();

$apps = DB::table('applications')
            ->leftJoin('jobseekers', 'applications.jobseeker_id', '=', 'jobseekers.id')
            ->groupby('jobseekers.id')
            ->whereIn('applications.job_id', $jobs)
            ->get();

     $apps_array = array();
     for ($i = 0; $i < count($apps); $i++) {
        $apps_array[$i] = (array) $apps[$i];
     }

     $jobseeker = array_merge($jobseekers, $apps_array);

     return view('frontend.manageCV', compact('jobseeker', 'assignJS'));

1 个答案:

答案 0 :(得分:1)

试试这个,看看它是怎么回事

// I have changed the get to paginate
$jobseekers = Jobseeker::where('employer_id', Auth::employer()->get()->id)->paginate();

$jobs = Job::select('id')->where('employer_id', (Auth::employer()->get()->id))->get()->toArray();

$apps = DB::table('applications')
        ->leftJoin('jobseekers', 'applications.jobseeker_id', '=', 'jobseekers.id')
        ->groupby('jobseekers.id')
        ->whereIn('applications.job_id', $jobs)
        ->get();

 $apps_array = array();
 for ($i = 0; $i < count($apps); $i++) {
    $apps_array[$i] = (array) $apps[$i];
 }

 // I am merging to the $jobseekers->data in $jobseekers array
 $jobseekers->data = (object)array_merge((array)$jobseekers->data, $apps_array);

 return view('frontend.manageCV', compact('jobseekers', 'assignJS'));