Laravel - How to orderBy eloquent query with pivot and each function

Michał Lipa

I have a problem with orderBy in more complicated query.

I want to get 10 ordered users (pivot with club) from specified club and hide some columns.

So I have query in eloquent which almost works for me. The problem is with ordering users by some variable (level or exp - don't know yet).

My query (working - without ordering):

        $users = Club::find($club_id)->users->each(function($row) {

            $row->setHidden(['history', 'books', 'reviews', 'exp', 'erudion_id', 'phone', 'bio', 'active', 'book_transfer_type', 'email', 'password', 'remember_token', 'created_at', 'updated_at', 'active_club', 'facebook_id', 'password_token', 'points', 'password_status', 'pivot']);

        })->take(10);

    return $users;

One of my trials (with OrderBy):

    //
//        $users = Club::find($club_id)->with(['users' => function ($q) {
//
//            $q->orderBy('id');
//
//        }])->each(function($row) {
//
//            $row->setHidden(['history', 'books', 'reviews', 'exp', 'erudion_id', 'phone', 'bio', 'active', 'book_transfer_type', 'email', 'password', 'remember_token', 'created_at', 'updated_at', 'active_club', 'facebook_id', 'password_token', 'points', 'password_status', 'pivot']);
//
//        });
//
//        return $users;

Error:

The Response content must be a string or object implementing __toString(), "boolean" given.

I have appended attributes in my model. This attributes cannot be unselected by not including them in select, so they are basically always selected.

I want to create eloquent query which will limit attributes to only few, which interest me.

Michał Lipa

Ok, I figure it out in different way. Maybe it not looks so good but it works. Works much faster than without hidden attributes.

I use in my models appended attributes, which are always selected (even without selecting them in eloquent) so I had to create array with attributes which interest me.

  $users = Club::where('id', $club_id)->first()->users()->orderBy('id')->limit(10)->get();

    $ranking = [];

    foreach($users as $k => $user){
        $ranking[$k]['id'] = $user['id'];
        $ranking[$k]['f_name'] = $user['f_name'];
        ...
    }

    return $ranking;

Thank you guys for clues.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to query count on the pivot table in Laravel eloquent

how to get ->orderBy('holding' > 0) in laravel eloquent query?

Laravel 5: How to do a join query on a pivot table using Eloquent?

How to query pivot table with multiple foreign keys Laravel 5 Eloquent

How do I query a polymorphic pivot table with Eloquent & Laravel 5

Join and OrderBy within with laravel eloquent query

laravel:how to use LOWERCASE Function in Eloquent Query?

Getting rank of row in OrderBy desc Eloquent query, How can i make this query work in laravel 5.5 eloquents?)

How to Query Laravel Eloquent

How to get all Users orderBy their ratings using Laravel 5 / Eloquent / query builder

Laravel Eloquent ManyToMany with Pivot, secondary query with variables in pivot table?

Laravel Eloquent `take` and `orderBy`

Laravel eloquent orderby relation

OrderBy count appearing in the wrong order in Eloquent Query Builder (Laravel 4)

How to add laravel query function lists in updateOrCreate eloquent

Eloquent Query with Pivot Table

How to Make Laravel Eloquent "IN" Query?

Laravel Eloquent - How to query NOT LIKE?

Laravel - How to query Eloquent relationship?

How to build this Laravel Eloquent Query

How to Make Laravel Eloquent "AND" in a Query?

How to write this query in Laravel Eloquent

laravel eloquent pivot table query "ambiguous column name"

Laravel eloquent with pivot table

Laravel Eloquent - Pivot Table

Laravel Query | How to set OrderBy to a new array?

Laravel 5: How to use 'where' or 'orderBy' using eloquent?

how to make orderBy ignore value 0 in eloquent laravel

Laravel Eloquent - __call() function to get query