1

I want to add pagination in my custom laravel query. Here is my code.

$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'));
2
  • Were is the $apps variable coming from. Commented Mar 9, 2016 at 11:10
  • i just define at 3rd line Commented Mar 9, 2016 at 11:17

1 Answer 1

1

Try this and let see how it goes

// 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'));
Sign up to request clarification or add additional context in comments.

7 Comments

but it will give error in template : Call to a member function render() on a non-object
If you are using it in a view then you dont have to call the toArray on the Collection. Updating my answer.
Also in total it count only $jobseekers data. Cant consider $apps_array record
Wish i can see your models and the final results after the merging.
its give error at merge line : Undefined property: Illuminate\Pagination\LengthAwarePaginator::$data
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.