skip to Main Content

I’m working on a Laravel project where I have two collections of jobs: one paginated and one static. Here’s the code in my controller:

$jobs = Job::with('employer', 'tags')
    ->latest()
    ->paginate(10);

$futuredJobs = Job::with('employer', 'tags')
    ->latest()
    ->where('featured', 1)->latest()
    ->paginate(6);

$tags = Tag::all();

return view('job.index', ['jobs' => $jobs, 'tags' => $tags, 'futuredJobs' => $futuredJobs]);

In my Blade template, I use the paginator to display the paginated jobs:

@foreach ($jobs as $job)
    <!-- Display job details -->
@endforeach

{{ $jobs->links() }}

@foreach ($futuredJobs as $featuredJob)
    <!-- Display featured job details -->
@endforeach

2

Answers


  1. Wat means static and non static? U have 2 similar collections, just different data. Also u have doubled latest() in second one

    Login or Signup to reply.
  2. Not sure what your problem is but from what I understand use get() instead of paginate(6) in $futureJobs.

    If you want to show only latest 6 jobs use

    ->get()
    ->take(6)
    

    Note that

    Also there are 2 latest() in $futureJobs only use one.

    Also compact('jobs','tags','futuredJobs') instead of ['jobs' => $jobs, 'tags' => $tags, 'futuredJobs' => $futuredJobs] can be used while returning variables to view.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search