How to order by last name in laravel controller return value using foreach loop

I tried to put orderBy(hr_workers.l_name) before get() but it’s not working.. also tried to put it on $workers but still not working… I don’t have any idea why isn’t working…
anyone can help me all answer must be appreciated thanks in advance

I want the result is alphabetically arrange by last name

public function show6($id)
    {

    $workers = DB::table('hr_workers')
    ->select('wrk_id','f_name','m_name','l_name');

    $attendance = DB::table('payroll_daily_attendance')->where('payroll_daily_id', $id)
    ->select('payroll_daily_attendance.*','f_name','l_name','m_name')
    ->rightjoinSub($workers,'worker', function($join){
        $join->on('payroll_daily_attendance.wrk_id','=','worker.wrk_id');
    });
   
    $payroll = DB::table('payroll_daily_details')
    ->select('payroll_daily_details.*','wrk_id','f_name','m_name','l_name','attendance.*')
    ->rightjoinSub($attendance,'attendance', function($join){
        $join->on('payroll_daily_details.payroll_daily_id','=','attendance.payroll_daily_id');
    })
    
    ->get();

    $fetch = [];
    foreach($payroll as $key){
         if(!isset($fetch[$key->wrk_id]['total_ot']) && !isset($fetch[$key->wrk_id]['total_days']) && !isset($fetch[$key->wrk_id]['grand_total'])){
            $fetch[$key->wrk_id]['total_ot'] = 0;
            $fetch[$key->wrk_id]['total_days'] = 0;
            $fetch[$key->wrk_id]['total_allowance'] = 0;
            $fetch[$key->wrk_id]['grand_total'] = 0;
          }
            $fetch[$key->wrk_id]['wrk_id'] = $key->wrk_id;
            $fetch[$key->wrk_id]['f_name'] = $key->f_name;
            $fetch[$key->wrk_id]['l_name'] = $key->l_name;
            $fetch[$key->wrk_id]['m_name'] = $key->m_name;
            $fetch[$key->wrk_id]['total_days'] += $key->reg_hour + $key->adj_hour;
            $fetch[$key->wrk_id]['total_allowance'] += $key->allowance;
         
          
      }
      return $fetch;


    }

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Because there can be multiple l_name values associated with each wrk_id in the $fetch array, any l_name ordering in the query will be overwritten because of the way the $fetch array is being generated. You need to sort the $fetch array by the l_name value before you return it, which you can do using this method (derived from this Q&A):

usort($fetch, function($a, $b) { return $a['l_name'] <=> $b['l_name']; });

Method 2

Use:

$payroll = DB::table('payroll_daily_details')
->select('payroll_daily_details.*','wrk_id','f_name','m_name','l_name','attendance.*')
->rightjoinSub($attendance,'attendance', function($join){
    $join->on('payroll_daily_details.payroll_daily_id','=','attendance.payroll_daily_id');
})
->orderBy('l_name')
->get();

Method 3

As was highlighted by Euclides Cardoso Junior. I believe for you to have your results arranged alphabetically you need to put the second parameter to the orderBy function to entail the order to be followed and also put the group by function. Check the code below;
$yourQuery->orderBy('l_name', 'asc') ->groupBy('l_name') ->get();

Hope this is helpful.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x