Putting order by inside with(), in Laravel

laravel eloquent order by relation column
laravel orderby belongsto relationship
laravel order by nested relationship
laravel order by relation column
laravel sortby relationship column
laravel order by created_at
laravel order parent by child
laravel implode

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();

Eloquent Relations: how to order topics by newest posts?, You can easily order Eloquent results by some column, but what if you want to order by related public function posts() { return $this->hasMany(\App\Post::​class); } Currently, I'm doing this by putting posts count in an array and then sort it. Hi Povilas, is this possible to do within a global query scope? Laravel order by ASC DESC. DB::table(‘table_name’)->orderBy(‘column_name’,’DESC_ASC’)->get(); is used for order by condition in laravel. Where first parameter is column name and second parameter is direction of order ie. DESC or ASC. We are going to fetch data with orderBy clause in laravel from the table emp.


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;

Sorting Nested Laravel Collections (Example), A protip by searsaw about php and laravel. using Eloquent to retrieve the customer and its transactions using the with() function. This returned a collection of transaction objects in the order they were put into the database. 7 Answers 7. The best way to create a controller is to use the built in Laravel utility, Artisan. From a command prompt, browse to the directory your laravel project is located. This will generate a file named showDashboard.php within an admin directory under your controllers.


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.

Eloquent order by related table, I need to be able to order it by the picture count. What about query scopes? http​://laravel.com/docs/4.2/eloquent#query-scopes Unfortunately, a SQL join (with Eloquent) will mess up the ->count(). them heavily, but only for an elegant and fluent substitute to complex where's (within a single query). Laravel provides an updateOrCreate method to do this in one step. Like the firstOrCreate method, updateOrCreate persists the model, so there's no need to call save() : // If there's a flight from Oakland to San Diego, set the price to $99.


How to renumber order by column in database with Laravel, In case anyone runs into this issue, I solved it by putting the sql code into a stored procedure and calling the stored procedure through Laravel. Basic Routing. The most basic Laravel routes accept a URI and a Closure, providing a very simple and expressive method of defining routes: Route::get('foo', function { return 'Hello World'; }); The Default Route Files. All Laravel routes are defined in your route files, which are located in the routes directory. These files are automatically loaded by the framework.


orderBy table using relation data : laravel, Is there a way to sort the foo table by it's corresponding record's username file_get_contents on a file then pass it into the put() function it'll store the file, but  Blade is the simple, yet powerful templating engine provided with Laravel. Unlike other popular PHP templating engines, Blade does not restrict you from using plain PHP code in your views. In fact, all Blade views are compiled into plain PHP code and cached until they are modified, meaning Blade adds essentially zero overhead to your application.


Laravel, Sort(). The sort method sorts the collection: $collection = collect([5, 3, 1, 2, 4]); $​sorted = $collection->sort(); echo $sorted->values()->all(); returns : [1, 2, 3, 4, 5]. Laravel Vapor is a serverless deployment platform for Laravel, powered by AWS. Launch your Laravel infrastructure on Vapor and fall in love with the scalable simplicity of serverless. Revolutionize how you build the web. The Laravel Ecosystem. Vapor Serverless Platform. Forge Server Management. Envoyer Zero Downtime Deployment.