N
N
Nilmir2018-05-11 13:05:34
Laravel
Nilmir, 2018-05-11 13:05:34

Laravel sorting a selection from a database using sortBy?

There is a simple model

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;

class Workers extends Model
{
    public $timestamps = false;
}

And controller
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Workers;

class WorkersController extends Controller
{
    function orderBy(Request $request )
    {          
        $data =Workers::all()->sortBy('name');
        dd($data[0],$data[1],$data[2]);
    }
}

Sorting is not applied records are still sorted by id. Tell me what's wrong?
Edith. Screenshot of the first records in the table.
5af5748c48b0c199488646.png
Result of give dump dd():
Workers {#52941
  +timestamps: false
  #connection: "mysql"
  #table: null
  #primaryKey: "id"
  #keyType: "int"
  +incrementing: true
  #with: []
  #withCount: []
  #perPage: 15
  +exists: true
  +wasRecentlyCreated: false
  #attributes: array:7 [
    "id" => 1
    "name" => "Bulah Carroll"
    "position" => "et"
    "salary" => 18880
    "work_permit" => "1984-03-21"
    "parent_id" => 0
    "tree_lvl" => 1
  ]
  #original: array:7 [
    "id" => 1
    "name" => "Bulah Carroll"
    "position" => "et"
    "salary" => 18880
    "work_permit" => "1984-03-21"
    "parent_id" => 0
    "tree_lvl" => 1
  ]
  #changes: []
  #casts: []
  #dates: []
  #dateFormat: null
  #appends: []
  #dispatchesEvents: []
  #observables: []
  #relations: []
  #touches: []
  #hidden: []
  #visible: []
  #fillable: []
  #guarded: array:1 [
    0 => "*"
  ]
}

Workers {#52942
  +timestamps: false
  #connection: "mysql"
  #table: null
  #primaryKey: "id"
  #keyType: "int"
  +incrementing: true
  #with: []
  #withCount: []
  #perPage: 15
  +exists: true
  +wasRecentlyCreated: false
  #attributes: array:7 [
    "id" => 2
    "name" => "Jaylan Wisozk PhD"
    "position" => "consequuntur"
    "salary" => 18922
    "work_permit" => "2006-07-28"
    "parent_id" => 1
    "tree_lvl" => 2
  ]
  #original: array:7 [
    "id" => 2
    "name" => "Jaylan Wisozk PhD"
    "position" => "consequuntur"
    "salary" => 18922
    "work_permit" => "2006-07-28"
    "parent_id" => 1
    "tree_lvl" => 2
  ]
  #changes: []
  #casts: []
  #dates: []
  #dateFormat: null
  #appends: []
  #dispatchesEvents: []
  #observables: []
  #relations: []
  #touches: []
  #hidden: []
  #visible: []
  #fillable: []
  #guarded: array:1 [
    0 => "*"
  ]
}

Workers {#52943
  +timestamps: false
  #connection: "mysql"
  #table: null
  #primaryKey: "id"
  #keyType: "int"
  +incrementing: true
  #with: []
  #withCount: []
  #perPage: 15
  +exists: true
  +wasRecentlyCreated: false
  #attributes: array:7 [
    "id" => 3
    "name" => "Jordi Gusikowski"
    "position" => "vel"
    "salary" => 19172
    "work_permit" => "1973-01-26"
    "parent_id" => 1
    "tree_lvl" => 2
  ]
  #original: array:7 [
    "id" => 3
    "name" => "Jordi Gusikowski"
    "position" => "vel"
    "salary" => 19172
    "work_permit" => "1973-01-26"
    "parent_id" => 1
    "tree_lvl" => 2
  ]
  #changes: []
  #casts: []
  #dates: []
  #dateFormat: null
  #appends: []
  #dispatchesEvents: []
  #observables: []
  #relations: []
  #touches: []
  #hidden: []
  #visible: []
  #fillable: []
  #guarded: array:1 [
    0 => "*"
  ]
}

Workers {#52944
  +timestamps: false
  #connection: "mysql"
  #table: null
  #primaryKey: "id"
  #keyType: "int"
  +incrementing: true
  #with: []
  #withCount: []
  #perPage: 15
  +exists: true
  +wasRecentlyCreated: false
  #attributes: array:7 [
    "id" => 4
    "name" => "Magnus Christiansen"
    "position" => "et"
    "salary" => 14109
    "work_permit" => "1981-12-01"
    "parent_id" => 1
    "tree_lvl" => 2
  ]
  #original: array:7 [
    "id" => 4
    "name" => "Magnus Christiansen"
    "position" => "et"
    "salary" => 14109
    "work_permit" => "1981-12-01"
    "parent_id" => 1
    "tree_lvl" => 2
  ]
  #changes: []
  #casts: []
  #dates: []
  #dateFormat: null
  #appends: []
  #dispatchesEvents: []
  #observables: []
  #relations: []
  #touches: []
  #hidden: []
  #visible: []
  #fillable: []
  #guarded: array:1 [
    0 => "*"
  ]
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Y
Yan-s, 2018-05-11
@Nilmir

Let's create a collection of elements with names:
Dump and see what we have there:

Collection {#581 ▼
  #items: array:3 [▼
    0 => array:1 [▼
      "name" => "c"
    ]
    1 => array:1 [▼
      "name" => "b"
    ]
    2 => array:1 [▼
      "name" => "a"
    ]
  ]
}

Now let's sort and dump again :dd($collection->sortBy('name'));
Collection {#566 ▼
  #items: array:3 [▼
    2 => array:1 [▼
      "name" => "a"
    ]
    1 => array:1 [▼
      "name" => "b"
    ]
    0 => array:1 [▼
      "name" => "c"
    ]
  ]
}

As you can see, the order in the collection has changed, now it is sorted by name. But, the keys remained the same, so the call dd($data[0],$data[1],$data[2]); will output the values ​​in the old order.
That is, the sorting will be preserved during the ordinal traversal of the collection, accessing the key you violate this order.
The values() method will return a new collection in which the keys will be reset and numbered again, in order, that is, the keys will correspond to sorting.
If you get a collection from the database, then it is more efficient to sort directly on the database side, in other cases, consider the described behavior of collections.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question