orderBy doesn't work laravel in one table

linq orderby multiple ascending descending
linq orderby ascending
order by ascending in linq lambda c#
orderby c# multiple fields
iqueryable orderby
c# list sort descending
order by and then by c#
string orderby c#

I'd like to sort my data by using orderby in Laravel. Here is my code:

History::where('cus_id', $id)
    ->orderBy('updated_at', 'DESC')
    ->get();

History table migration

public function up()
{
    if(!Schema::hasTable('history')) {
        Schema::create('history', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name', 255)->nullable();
            $table->string('cus_id', 40)->nullable();
            $table->string('activity', 255)->nullable();
            $table->string('remark_id', 4)->nullable();
            $table->integer('user_id')->unsigned()->nullable();
            $table->string('note', 255)->nullable();
            $table->timestamps();
        });
    }
}

the result is not according to orderby

OrderBy & OrderByDescending - Sorting Operators, Method syntax does not allow the decending keyword to sorts the collection in decending order. Use OrderByDecending() method for it. OrderByDescending. Pinal Dave is a SQL Server Performance Tuning Expert and an independent consultant. He has authored 12 SQL Server database books, 33 Pluralsight courses and has written over 5100 articles on the database technology on his blog at a https://blog.sqlauthority.com. Along with 17+ years of hands-on experience, he holds a Masters of Science degree and a number of database certifications.

The orderBy method allows you to sort the result of the query by a given column. The first argument to the orderBy method should be the column you wish to sort by, while the second argument controls the direction of the sort and may be either asc or desc =>

Here is the solution for you =>

 $users = DB::table('history')
                    ->where(['cus_id'=>$id])    
                    ->orderBy('updated_at', 'desc')
                    ->get();

If you are using model then,

History::where('cus_id',$id)->orderBy('updated_at','desc')->get();

OrderBy doesn't work if ThenInclude uses (or how to order nested , OrderBy doesn't work if ThenInclude uses (or how to order nested collection) #​9445. Closed. neooleg opened this issue on Aug 17, 2017 · 8 comments. Closed​  Because IOrderedEnumerable<TElement> inherits from IEnumerable<T>, you can call OrderBy or OrderByDescending on the results of a call to OrderBy, OrderByDescending, ThenBy or ThenByDescending. Doing this introduces a new primary ordering that ignores the previously established ordering.

Why don't you try some alternate solution? Use sortbyDesc method, I confirmed from documentation that it is available for your version of Laravel(5.4).

Here goes your code:

History::where('cus_id', $id)
    ->get()  
    ->sortbyDesc('updated_at');

orderBy doesn't work · Issue #223 · laravel/scout · GitHub, Hi there, I'm using scout with algolia to index a large database of products, however when trying to sort the results, i'm not getting the desired results. $​products  In a query expression, the orderby clause causes the returned sequence or subsequence (group) to be sorted in either ascending or descending order. Multiple keys can be specified in order to perform one or more secondary sort operations. The sorting is performed by the default comparer for the type of the element.

3.7. ORDER BY - Chapter 3. Clauses, Introduction. Note that you cannot sort on nodes or relationships, just on properties on these. ORDER BY relies on comparisons to sort  OrderBy extension method has two overloads. First overload of OrderBy extension method accepts the Func delegate type parameter. So you need to pass the lambda expression for the field based on which you want to sort the collection.

ORDER BY Clause (Transact-SQL), The OVER clause does not support OFFSET and FETCH. OFFSET and FETCH cannot be specified directly in INSERT, UPDATE, MERGE, and  The LIMIT and OFFSET options can be used without an ORDER BY clause; however, to return a consistent set of rows, use these options in conjunction with ORDER BY. In any parallel system like Amazon Redshift, when ORDER BY doesn't produce a unique ordering, the order of the rows is nondeterministic.

Orderby doesn't work like expected on custom query, First of all, never ever use query_posts . It breaks the main function and all plugins and functions relying on the main query object. For custom  Each clause has sort criteria, optionally followed by a sort direction (asc for ascending or desc for descending).If you don't specify a direction, the default is ascending.

Comments
  • try using the lowercase 'desc'
  • Can you post your create history table migration?
  • @EricMarcelino the results are the same, not in the order
  • @Peter history table migration above
  • @ialx what version of laravel you are using?
  • This still doesn't answer why orderBy is not working. To me, it would be cleaner to have everything handled in one single query than to fetch data and to map the Collection afterward.