Laravel sortBy() not working to sort a collection
up vote
0
down vote
favorite
I want to sort a collection by name column. Using sortBy() method is not working.
$collection = AppHttpResourcesMyResource::collection($test);
return $collection->sortBy('name');
laravel
add a comment |
up vote
0
down vote
favorite
I want to sort a collection by name column. Using sortBy() method is not working.
$collection = AppHttpResourcesMyResource::collection($test);
return $collection->sortBy('name');
laravel
Please, explain better in that way sortBy() doesn't work. Exception? Empty collection?
– Tudor
Nov 17 at 22:05
It returns a collection without sorting it. Like there is no sortBy().
– Josh
Nov 17 at 22:09
Can you give an example of the data that you are trying to sort?
– nakov
Nov 17 at 22:11
The name column is simple words, they are names.
– Josh
Nov 17 at 22:12
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I want to sort a collection by name column. Using sortBy() method is not working.
$collection = AppHttpResourcesMyResource::collection($test);
return $collection->sortBy('name');
laravel
I want to sort a collection by name column. Using sortBy() method is not working.
$collection = AppHttpResourcesMyResource::collection($test);
return $collection->sortBy('name');
laravel
laravel
asked Nov 17 at 22:02
Josh
267
267
Please, explain better in that way sortBy() doesn't work. Exception? Empty collection?
– Tudor
Nov 17 at 22:05
It returns a collection without sorting it. Like there is no sortBy().
– Josh
Nov 17 at 22:09
Can you give an example of the data that you are trying to sort?
– nakov
Nov 17 at 22:11
The name column is simple words, they are names.
– Josh
Nov 17 at 22:12
add a comment |
Please, explain better in that way sortBy() doesn't work. Exception? Empty collection?
– Tudor
Nov 17 at 22:05
It returns a collection without sorting it. Like there is no sortBy().
– Josh
Nov 17 at 22:09
Can you give an example of the data that you are trying to sort?
– nakov
Nov 17 at 22:11
The name column is simple words, they are names.
– Josh
Nov 17 at 22:12
Please, explain better in that way sortBy() doesn't work. Exception? Empty collection?
– Tudor
Nov 17 at 22:05
Please, explain better in that way sortBy() doesn't work. Exception? Empty collection?
– Tudor
Nov 17 at 22:05
It returns a collection without sorting it. Like there is no sortBy().
– Josh
Nov 17 at 22:09
It returns a collection without sorting it. Like there is no sortBy().
– Josh
Nov 17 at 22:09
Can you give an example of the data that you are trying to sort?
– nakov
Nov 17 at 22:11
Can you give an example of the data that you are trying to sort?
– nakov
Nov 17 at 22:11
The name column is simple words, they are names.
– Josh
Nov 17 at 22:12
The name column is simple words, they are names.
– Josh
Nov 17 at 22:12
add a comment |
3 Answers
3
active
oldest
votes
up vote
0
down vote
If the Laravel default sorting doesn't work for your purpose, try to do this:
$collection = AppHttpResourcesMyResource::collection->sortBy(function ($element, $key) {
return yourFunctionForSorting($element['name']);
});
Replace obviously "yourFunctionForSorting", with your sorting criteria.
could you give an example of yourFunctionForSorting please, to sort alphabetically?
– Josh
Nov 17 at 22:19
@Josh Try to do this: substr($element['name'], 0, 1);
– Tudor
Nov 17 at 22:27
still not ordering
– Josh
Nov 17 at 22:31
Retry with this: $collection = AppHttpResourcesMyResource::collection->groupBy(function ($element, $key) { return substr($element['name'], 0, 1); });
– Tudor
Nov 17 at 22:32
still doesn't work
– Josh
Nov 17 at 22:36
|
show 2 more comments
up vote
0
down vote
The thing you want to do is:
$collection = AppHttpResourcesMyResource::collection($test);
return $collection->sortBy('name')->all();
Check how it is done in the docs.
same result, unordered
– Josh
Nov 17 at 22:49
Could you trydd($collection);
and post the results in your question. So we can see what is returned,
– Thomas Van der Veen
Nov 17 at 23:21
The data I'm trying to sort is inside "data" key, because the result is paginated. I think that's the problem. But how can I sort that? sortBy('data.name') didn't work.
– Josh
Nov 19 at 19:59
add a comment |
up vote
0
down vote
The default sortBy for Laravel is defined as ascending:
public function sortBy($callback, $options = SORT_REGULAR, $descending = false)
If you were looking to sort descending use sortByDesc
:
public function sortByDesc($callback, $options = SORT_REGULAR)
{
return $this->sortBy($callback, $options, true);
}
Where the first parameter $callback
can be a string
or a callable
, so your code could look something like this with items sorted descending:
$collection = AppHttpResourcesMyResource::collection($test);
return $collection->sortByDesc('name')->all();
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
If the Laravel default sorting doesn't work for your purpose, try to do this:
$collection = AppHttpResourcesMyResource::collection->sortBy(function ($element, $key) {
return yourFunctionForSorting($element['name']);
});
Replace obviously "yourFunctionForSorting", with your sorting criteria.
could you give an example of yourFunctionForSorting please, to sort alphabetically?
– Josh
Nov 17 at 22:19
@Josh Try to do this: substr($element['name'], 0, 1);
– Tudor
Nov 17 at 22:27
still not ordering
– Josh
Nov 17 at 22:31
Retry with this: $collection = AppHttpResourcesMyResource::collection->groupBy(function ($element, $key) { return substr($element['name'], 0, 1); });
– Tudor
Nov 17 at 22:32
still doesn't work
– Josh
Nov 17 at 22:36
|
show 2 more comments
up vote
0
down vote
If the Laravel default sorting doesn't work for your purpose, try to do this:
$collection = AppHttpResourcesMyResource::collection->sortBy(function ($element, $key) {
return yourFunctionForSorting($element['name']);
});
Replace obviously "yourFunctionForSorting", with your sorting criteria.
could you give an example of yourFunctionForSorting please, to sort alphabetically?
– Josh
Nov 17 at 22:19
@Josh Try to do this: substr($element['name'], 0, 1);
– Tudor
Nov 17 at 22:27
still not ordering
– Josh
Nov 17 at 22:31
Retry with this: $collection = AppHttpResourcesMyResource::collection->groupBy(function ($element, $key) { return substr($element['name'], 0, 1); });
– Tudor
Nov 17 at 22:32
still doesn't work
– Josh
Nov 17 at 22:36
|
show 2 more comments
up vote
0
down vote
up vote
0
down vote
If the Laravel default sorting doesn't work for your purpose, try to do this:
$collection = AppHttpResourcesMyResource::collection->sortBy(function ($element, $key) {
return yourFunctionForSorting($element['name']);
});
Replace obviously "yourFunctionForSorting", with your sorting criteria.
If the Laravel default sorting doesn't work for your purpose, try to do this:
$collection = AppHttpResourcesMyResource::collection->sortBy(function ($element, $key) {
return yourFunctionForSorting($element['name']);
});
Replace obviously "yourFunctionForSorting", with your sorting criteria.
answered Nov 17 at 22:15
Tudor
3131415
3131415
could you give an example of yourFunctionForSorting please, to sort alphabetically?
– Josh
Nov 17 at 22:19
@Josh Try to do this: substr($element['name'], 0, 1);
– Tudor
Nov 17 at 22:27
still not ordering
– Josh
Nov 17 at 22:31
Retry with this: $collection = AppHttpResourcesMyResource::collection->groupBy(function ($element, $key) { return substr($element['name'], 0, 1); });
– Tudor
Nov 17 at 22:32
still doesn't work
– Josh
Nov 17 at 22:36
|
show 2 more comments
could you give an example of yourFunctionForSorting please, to sort alphabetically?
– Josh
Nov 17 at 22:19
@Josh Try to do this: substr($element['name'], 0, 1);
– Tudor
Nov 17 at 22:27
still not ordering
– Josh
Nov 17 at 22:31
Retry with this: $collection = AppHttpResourcesMyResource::collection->groupBy(function ($element, $key) { return substr($element['name'], 0, 1); });
– Tudor
Nov 17 at 22:32
still doesn't work
– Josh
Nov 17 at 22:36
could you give an example of yourFunctionForSorting please, to sort alphabetically?
– Josh
Nov 17 at 22:19
could you give an example of yourFunctionForSorting please, to sort alphabetically?
– Josh
Nov 17 at 22:19
@Josh Try to do this: substr($element['name'], 0, 1);
– Tudor
Nov 17 at 22:27
@Josh Try to do this: substr($element['name'], 0, 1);
– Tudor
Nov 17 at 22:27
still not ordering
– Josh
Nov 17 at 22:31
still not ordering
– Josh
Nov 17 at 22:31
Retry with this: $collection = AppHttpResourcesMyResource::collection->groupBy(function ($element, $key) { return substr($element['name'], 0, 1); });
– Tudor
Nov 17 at 22:32
Retry with this: $collection = AppHttpResourcesMyResource::collection->groupBy(function ($element, $key) { return substr($element['name'], 0, 1); });
– Tudor
Nov 17 at 22:32
still doesn't work
– Josh
Nov 17 at 22:36
still doesn't work
– Josh
Nov 17 at 22:36
|
show 2 more comments
up vote
0
down vote
The thing you want to do is:
$collection = AppHttpResourcesMyResource::collection($test);
return $collection->sortBy('name')->all();
Check how it is done in the docs.
same result, unordered
– Josh
Nov 17 at 22:49
Could you trydd($collection);
and post the results in your question. So we can see what is returned,
– Thomas Van der Veen
Nov 17 at 23:21
The data I'm trying to sort is inside "data" key, because the result is paginated. I think that's the problem. But how can I sort that? sortBy('data.name') didn't work.
– Josh
Nov 19 at 19:59
add a comment |
up vote
0
down vote
The thing you want to do is:
$collection = AppHttpResourcesMyResource::collection($test);
return $collection->sortBy('name')->all();
Check how it is done in the docs.
same result, unordered
– Josh
Nov 17 at 22:49
Could you trydd($collection);
and post the results in your question. So we can see what is returned,
– Thomas Van der Veen
Nov 17 at 23:21
The data I'm trying to sort is inside "data" key, because the result is paginated. I think that's the problem. But how can I sort that? sortBy('data.name') didn't work.
– Josh
Nov 19 at 19:59
add a comment |
up vote
0
down vote
up vote
0
down vote
The thing you want to do is:
$collection = AppHttpResourcesMyResource::collection($test);
return $collection->sortBy('name')->all();
Check how it is done in the docs.
The thing you want to do is:
$collection = AppHttpResourcesMyResource::collection($test);
return $collection->sortBy('name')->all();
Check how it is done in the docs.
answered Nov 17 at 22:42
Thomas Van der Veen
1,4491017
1,4491017
same result, unordered
– Josh
Nov 17 at 22:49
Could you trydd($collection);
and post the results in your question. So we can see what is returned,
– Thomas Van der Veen
Nov 17 at 23:21
The data I'm trying to sort is inside "data" key, because the result is paginated. I think that's the problem. But how can I sort that? sortBy('data.name') didn't work.
– Josh
Nov 19 at 19:59
add a comment |
same result, unordered
– Josh
Nov 17 at 22:49
Could you trydd($collection);
and post the results in your question. So we can see what is returned,
– Thomas Van der Veen
Nov 17 at 23:21
The data I'm trying to sort is inside "data" key, because the result is paginated. I think that's the problem. But how can I sort that? sortBy('data.name') didn't work.
– Josh
Nov 19 at 19:59
same result, unordered
– Josh
Nov 17 at 22:49
same result, unordered
– Josh
Nov 17 at 22:49
Could you try
dd($collection);
and post the results in your question. So we can see what is returned,– Thomas Van der Veen
Nov 17 at 23:21
Could you try
dd($collection);
and post the results in your question. So we can see what is returned,– Thomas Van der Veen
Nov 17 at 23:21
The data I'm trying to sort is inside "data" key, because the result is paginated. I think that's the problem. But how can I sort that? sortBy('data.name') didn't work.
– Josh
Nov 19 at 19:59
The data I'm trying to sort is inside "data" key, because the result is paginated. I think that's the problem. But how can I sort that? sortBy('data.name') didn't work.
– Josh
Nov 19 at 19:59
add a comment |
up vote
0
down vote
The default sortBy for Laravel is defined as ascending:
public function sortBy($callback, $options = SORT_REGULAR, $descending = false)
If you were looking to sort descending use sortByDesc
:
public function sortByDesc($callback, $options = SORT_REGULAR)
{
return $this->sortBy($callback, $options, true);
}
Where the first parameter $callback
can be a string
or a callable
, so your code could look something like this with items sorted descending:
$collection = AppHttpResourcesMyResource::collection($test);
return $collection->sortByDesc('name')->all();
add a comment |
up vote
0
down vote
The default sortBy for Laravel is defined as ascending:
public function sortBy($callback, $options = SORT_REGULAR, $descending = false)
If you were looking to sort descending use sortByDesc
:
public function sortByDesc($callback, $options = SORT_REGULAR)
{
return $this->sortBy($callback, $options, true);
}
Where the first parameter $callback
can be a string
or a callable
, so your code could look something like this with items sorted descending:
$collection = AppHttpResourcesMyResource::collection($test);
return $collection->sortByDesc('name')->all();
add a comment |
up vote
0
down vote
up vote
0
down vote
The default sortBy for Laravel is defined as ascending:
public function sortBy($callback, $options = SORT_REGULAR, $descending = false)
If you were looking to sort descending use sortByDesc
:
public function sortByDesc($callback, $options = SORT_REGULAR)
{
return $this->sortBy($callback, $options, true);
}
Where the first parameter $callback
can be a string
or a callable
, so your code could look something like this with items sorted descending:
$collection = AppHttpResourcesMyResource::collection($test);
return $collection->sortByDesc('name')->all();
The default sortBy for Laravel is defined as ascending:
public function sortBy($callback, $options = SORT_REGULAR, $descending = false)
If you were looking to sort descending use sortByDesc
:
public function sortByDesc($callback, $options = SORT_REGULAR)
{
return $this->sortBy($callback, $options, true);
}
Where the first parameter $callback
can be a string
or a callable
, so your code could look something like this with items sorted descending:
$collection = AppHttpResourcesMyResource::collection($test);
return $collection->sortByDesc('name')->all();
answered Nov 18 at 0:05
adam
456410
456410
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53355957%2flaravel-sortby-not-working-to-sort-a-collection%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Please, explain better in that way sortBy() doesn't work. Exception? Empty collection?
– Tudor
Nov 17 at 22:05
It returns a collection without sorting it. Like there is no sortBy().
– Josh
Nov 17 at 22:09
Can you give an example of the data that you are trying to sort?
– nakov
Nov 17 at 22:11
The name column is simple words, they are names.
– Josh
Nov 17 at 22:12