在Laravel中用()内部命令

I have a controller that filters the columns in it's table based on the variable received in the get query, then it returns it with another table's value based on the id of the variable gotten, But now for the $filter_by_name condition I want to filter by the first_name column in the users table, please how can i do that, i.e I want to return the users table ordered by their first_name column

DB-STRUCTURE

COMPANY-USERS TABLE

id  company_id  user_id role created modified active department_level

USERS TABLE

id 
first_name 
last_name 
email 
password 
active_company 
profile_photo_id 
verified 
active 
remember_token 
created 
modified

Company-users Controller

public function getCompanyUsers($companyId)
{
    $filter = strtolower(Input::get('filter'));
    if($filter && $filter === 'on' ){
        $filter_by_date = strtolower(Input::get('filter_by_date'));
        $filter_by_name = strtolower(Input::get('filter_by_name'));
        $filter_by_role = strtolower(Input::get('filter_by_role'));
        if($filter_by_date){
            if($filter_by_date == 'oldest'){
                $users = CompanyUser::where('company_id', $companyId)->orderBy('created', 'DESC')
                ->with(['user','user.userDepartments','user.userDepartments.department'])->get();
                return $users;
            }else{
                $users = CompanyUser::where('company_id', $companyId)->orderBy('created', 'ASC')
                    ->with(['user','user.userDepartments','user.userDepartments.department'])->get();
                return $users;
            }
        }elseif ($filter_by_name){
            if($filter_by_name == 'ascending'){
                $users = CompanyUser::where('company_id', $companyId)->orderBy('first_name', 'ASC')
                    ->with(['user','user.userDepartments','user.userDepartments.department'])->get();
                return $users;
            }else{
                $users = CompanyUser::where('company_id', $companyId)->orderBy('first_name', 'DESC')
                    ->with(['user','user.userDepartments','user.userDepartments.department'])->get();
                return $users;
            }
        }elseif($filter_by_role){
            if($filter_by_role == 'member'){

                $users = CompanyUser::where(['company_id' => $companyId,'role'=>'Member'])->with(['user','user.userDepartments','user.userDepartments.department'])->get();
              //  dd($users);
                return $users;
            }elseif($filter_by_role == 'manager'){
                $users = CompanyUser::where(['company_id' => $companyId,'role'=>'Manager'])->with(['user','user.userDepartments','user.userDepartments.department'])->get();
                return $users;
            }else
                $users = CompanyUser::where(['company_id' => $companyId,'role'=>'Admin'])->with(['user','user.userDepartments','user.userDepartments.department'])->get();
            return $users;
        }
    }
    $users = CompanyUser::where('company_id', $companyId)->
    with(['user','user.userDepartments','user.userDepartments.department'])->get();
    //dd($users);
    return $users;
}

you can pass closer functions when eager loading to add constrains like that, see more on laravel docs:

$users = CompanyUser::with(['user'=> function ($query) {
                                   $query->orderBy('first_name', 'desc');
                                 }
                           ])
                      ->where('company_id', $companyId)
                      ->get();

You have to do orderBy on a relational model like below:

 $users = CompanyUser::where('company_id', $companyId)
->with(['user' => function($subQuery){
     $subQuery->orderBy('first_name', 'ASC');
}])
->with(['user.userDepartments','user.userDepartments.department'])
->get();

return $users;

One way is to add column 'first_name' in the 'COMPANY-USERS' TABLE

And now you can do order by ('first_name','ASC'). I think its not the best way but no other idea comes in my head.