Answer the question
In order to leave comments, you need to log in
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;
}
<?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]);
}
}
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
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"
]
]
}
dd($collection->sortBy('name'));
Collection {#566 ▼
#items: array:3 [▼
2 => array:1 [▼
"name" => "a"
]
1 => array:1 [▼
"name" => "b"
]
0 => array:1 [▼
"name" => "c"
]
]
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question