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

Yassine Jennane

i have added a new array to my elements like this code :

 $elements= Code::get();
 
 foreach($elements as $element){
    $string = str_random(15);
    $element->total = $string; 
 }

I would like to return elements after set orderBy() on total using this code

return response()->json(['Status'=> '1','Elements' => $elements ]);
 

Thank you and i appreciate you help.

pr1nc3

Well this is not how foreach works actually. foreach creates a "mirror" of your array so doing any insertion on $element does not affect your initial array at all.

You need to insert your value directly in your array, so you can either use a counter or simpler a for loop.

I will modify your foreach to stay similar to your approach.

Judging by how you tried to insert the value in your array i assume you have a collection and not an array so if that's the case you can add this line before everything:

$elements = $elements->toArray(); //convert collection to array
     $counter = 0;
     foreach($elements as $element){
        $string = str_random(15);
        $elements[$counter]['total'] = $string;
        $counter++;
     }

Your $elements array now has the field total in each of the nested arrays.

Then you simple do:

array_multisort( array_column($elements, "total"), SORT_ASC, $elements);

If you want to sort in descending order simple use SORT_DESC

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

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

How can i orderby and groupby in one query in laravel

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

orderBy field name in laravel query

How to custom OrderBy in Laravel

how to query an array of objects in laravel

How to get an array in laravel query

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

Laravel how to add new field in query result

$query->set orderby type not working as expected

Join and OrderBy within with laravel eloquent query

Laravel Query Builder - groupBy, map, sortBy / orderBy

Laravel 5.4 Query Builder Implementing orderBy with groupBy

Laravel Query: orderBy not working with groupBy (with a joined table)

how set fixed where query in model laravel

Query firestore based on array contains and orderby

how to set new properties in an array of objects explicitly

How to set new properties or methods on a Java array?

How does [...new set(array)] works in javascript?

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

how to write orderBy query for a attribute with some condition like has relation or not, in Laravel

How to unset old array keys and set new array by values of that array?

How to return multilple returns or query in laravel -- I'm new in laravel

Laravel multiple orderBy from dynamic array input

How to query json array of objects in mysql and laravel

How query to array of categories to get posts in Laravel?

How to set and get array into cookie in laravel

In Laravel, how to combine where() and orderBy() when orderBy() is working on relationship?

How to ignore new lines in lodash orderBy