-3

My Controller Query to show the datas as Follows (in laravel)

public function index(Request $request)
{
    $data = $this->jobRepository->prepareJobData();
    $data['input'] = $request->all();

    return view('web.jobs.index')->with($data);
}

My PrepareJobdata fuction

 public function prepareJobData()
{
    $data['jobTypes'] = JobType::pluck('name', 'id');
    $data['jobCategories'] = JobCategory::pluck('name', 'id');
    $data['jobSkills'] = Skill::pluck('name', 'id');
    $data['genders'] = Job::NO_PREFERENCE;
    $data['careerLevels'] = CareerLevel::pluck('level_name', 'id');
    $data['functionalAreas'] = FunctionalArea::pluck('name', 'id');

    return $data;
}

2 Answers2

0

In your prepareJobData() method you can chain this:

->orderBy('created_at', 'desc')->get();

// or simply
->latest()->get(); // ordered by created_at

Edit: So, it seems to be doing some formating which might not be the best approach. But anyway, something like this should do the trick.

public function prepareJobData()
{
    $data['jobTypes'] = JobType::latest()->pluck('name', 'id');
    $data['jobCategories'] = JobCategory::latest()->pluck('name', 'id');
    $data['jobSkills'] = Skill::latest()->pluck('name', 'id');
    $data['genders'] = Job::NO_PREFERENCE;
    $data['careerLevels'] = CareerLevel::latest()->pluck('level_name', 'id');
    $data['functionalAreas'] = FunctionalArea::latest()->pluck('name', 'id');

    return $data;
}
P. K. Tharindu
  • 2,565
  • 3
  • 17
  • 34
  • how i can write that P. K. Tharindu – Joseph MAthew Mar 21 '21 at 06:36
  • public function prepareJobData() { $data['jobTypes'] = JobType::pluck('name', 'id'); $data['jobCategories'] = JobCategory::pluck('name', 'id'); $data['jobSkills'] = Skill::pluck('name', 'id'); $data['genders'] = Job::NO_PREFERENCE; $data['careerLevels'] = CareerLevel::pluck('level_name', 'id'); $data['functionalAreas'] = FunctionalArea::pluck('name', 'id'); return $data; } – Joseph MAthew Mar 21 '21 at 06:39
  • Check the updated answer :) – P. K. Tharindu Mar 21 '21 at 07:09
0

Try these

->orderBy('created_At', 'desc')->first();

or

->latest('created_At')->first();

Please use ->get() for the last if you don't want to return the last row only instead, multiple rows

  • public function prepareJobData() { $data['jobTypes'] = JobType::pluck('name', 'id'); $data['jobCategories'] = JobCategory::pluck('name', 'id'); $data['jobSkills'] = Skill::pluck('name', 'id'); $data['genders'] = Job::NO_PREFERENCE; $data['careerLevels'] = CareerLevel::pluck('level_name', 'id'); $data['functionalAreas'] = FunctionalArea::pluck('name', 'id'); return $data; } this is my function how i can write Robert Anthony S. Tribiana – Joseph MAthew Mar 21 '21 at 06:39
  • I updated my answer. check the last line. Do you need the last row only or multiple rows? – Robert Anthony S. Tribiana Mar 21 '21 at 06:49
  • its showing error Robert Anthony S. Tribiana – Joseph MAthew Mar 21 '21 at 06:50
  • its showing error Robert Anthony S. Tribiana – Joseph MAthew Mar 21 '21 at 06:55