My object array ($c) like this :
Array
(
[0] => stdClass Object
(
[id] => 3
[name] => chelsea.png
)
[1] => stdClass Object
(
[id] => 4
[name] => arsenal.png
)
[2] => stdClass Object
(
[id] => 5
[name] => mu.png
)
[3] => stdClass Object
(
[id] => 1
[name] => city.png
)
)
I want to sort it by id
If in php normal I try like this :
function sort_by_id( $a, $b )
{
if( $a->id == $b->id ){ return 0 ; }
return ($a->id < $b->id) ? -1 : 1;
}
usort($c,'sort_by_id');
echo '<pre>';print_r($c);echo '</pre>';
It works
But How can I implement it use laravel?
I try like this :
public function add($param)
{
...
usort($c, $this->sortById());
echo '<pre>';print_r($c);echo '</pre>';die();
...
}
private function sortById($a, $b)
{
if($a->id == $b->id) {
return 0 ;
}
return ($a->id < $b->id) ? -1 : 1;
}
It does not work
There exist error like this :
Undefined property ... $sortById ...
How can I solve it?
toArraymethods for most cases, so you can convert objects to arrays on the calls.