Laravel | groupBy returning as object
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I am trying to use a Laravel collection to return a groupBy
as an array. However, it always seems to be returned as an object no matter what. I have tried to do $posts->groupBy('category')->toArray()
but this seems to still return as an object. I have also tried $posts->groupBy('category')->all()
and still it is returning as an object.
I don't know if this is something to do with Laravel returning methods within the routes, but I need this to return as an array.
Here is the code:
public function getFeatures($id)
{
return Feature::query()->get()->groupBy('category')->toArray();
}
The actual code is working fine and I'm getting results back but it just doesn't seem to be converting to an array. Thanks.
laravel eloquent
add a comment |
I am trying to use a Laravel collection to return a groupBy
as an array. However, it always seems to be returned as an object no matter what. I have tried to do $posts->groupBy('category')->toArray()
but this seems to still return as an object. I have also tried $posts->groupBy('category')->all()
and still it is returning as an object.
I don't know if this is something to do with Laravel returning methods within the routes, but I need this to return as an array.
Here is the code:
public function getFeatures($id)
{
return Feature::query()->get()->groupBy('category')->toArray();
}
The actual code is working fine and I'm getting results back but it just doesn't seem to be converting to an array. Thanks.
laravel eloquent
What is your use case? Do you need them converted to array to return it to a view or through an API?
– HCK
Nov 23 '18 at 21:16
Can you add add(...)
of your output?
– Jonathon
Nov 23 '18 at 22:50
@Jonathon thedd()
actually says that it is an array. Does the response convert to an object by default?
– connormiotk96
Nov 23 '18 at 23:07
Can you show the output in your question, either by adding a screenshot or pasting it in there? Do you get any errors or anything or is it just that you need an array? Do you need each item within the outer array to also be an array too?
– Jonathon
Nov 23 '18 at 23:53
It may also be worth mentioning that if you're outputting this as JSON, an associative array in PHP will be represented as an object in JSON. In Javascript/JSON an array is sequential and numerically indexed, an object allows for other types keys (but doesn't guarantee order).
– Jonathon
Nov 23 '18 at 23:55
add a comment |
I am trying to use a Laravel collection to return a groupBy
as an array. However, it always seems to be returned as an object no matter what. I have tried to do $posts->groupBy('category')->toArray()
but this seems to still return as an object. I have also tried $posts->groupBy('category')->all()
and still it is returning as an object.
I don't know if this is something to do with Laravel returning methods within the routes, but I need this to return as an array.
Here is the code:
public function getFeatures($id)
{
return Feature::query()->get()->groupBy('category')->toArray();
}
The actual code is working fine and I'm getting results back but it just doesn't seem to be converting to an array. Thanks.
laravel eloquent
I am trying to use a Laravel collection to return a groupBy
as an array. However, it always seems to be returned as an object no matter what. I have tried to do $posts->groupBy('category')->toArray()
but this seems to still return as an object. I have also tried $posts->groupBy('category')->all()
and still it is returning as an object.
I don't know if this is something to do with Laravel returning methods within the routes, but I need this to return as an array.
Here is the code:
public function getFeatures($id)
{
return Feature::query()->get()->groupBy('category')->toArray();
}
The actual code is working fine and I'm getting results back but it just doesn't seem to be converting to an array. Thanks.
laravel eloquent
laravel eloquent
edited Nov 23 '18 at 23:06
Karl Hill
3,49132446
3,49132446
asked Nov 23 '18 at 20:58
connormiotk96connormiotk96
277
277
What is your use case? Do you need them converted to array to return it to a view or through an API?
– HCK
Nov 23 '18 at 21:16
Can you add add(...)
of your output?
– Jonathon
Nov 23 '18 at 22:50
@Jonathon thedd()
actually says that it is an array. Does the response convert to an object by default?
– connormiotk96
Nov 23 '18 at 23:07
Can you show the output in your question, either by adding a screenshot or pasting it in there? Do you get any errors or anything or is it just that you need an array? Do you need each item within the outer array to also be an array too?
– Jonathon
Nov 23 '18 at 23:53
It may also be worth mentioning that if you're outputting this as JSON, an associative array in PHP will be represented as an object in JSON. In Javascript/JSON an array is sequential and numerically indexed, an object allows for other types keys (but doesn't guarantee order).
– Jonathon
Nov 23 '18 at 23:55
add a comment |
What is your use case? Do you need them converted to array to return it to a view or through an API?
– HCK
Nov 23 '18 at 21:16
Can you add add(...)
of your output?
– Jonathon
Nov 23 '18 at 22:50
@Jonathon thedd()
actually says that it is an array. Does the response convert to an object by default?
– connormiotk96
Nov 23 '18 at 23:07
Can you show the output in your question, either by adding a screenshot or pasting it in there? Do you get any errors or anything or is it just that you need an array? Do you need each item within the outer array to also be an array too?
– Jonathon
Nov 23 '18 at 23:53
It may also be worth mentioning that if you're outputting this as JSON, an associative array in PHP will be represented as an object in JSON. In Javascript/JSON an array is sequential and numerically indexed, an object allows for other types keys (but doesn't guarantee order).
– Jonathon
Nov 23 '18 at 23:55
What is your use case? Do you need them converted to array to return it to a view or through an API?
– HCK
Nov 23 '18 at 21:16
What is your use case? Do you need them converted to array to return it to a view or through an API?
– HCK
Nov 23 '18 at 21:16
Can you add a
dd(...)
of your output?– Jonathon
Nov 23 '18 at 22:50
Can you add a
dd(...)
of your output?– Jonathon
Nov 23 '18 at 22:50
@Jonathon the
dd()
actually says that it is an array. Does the response convert to an object by default?– connormiotk96
Nov 23 '18 at 23:07
@Jonathon the
dd()
actually says that it is an array. Does the response convert to an object by default?– connormiotk96
Nov 23 '18 at 23:07
Can you show the output in your question, either by adding a screenshot or pasting it in there? Do you get any errors or anything or is it just that you need an array? Do you need each item within the outer array to also be an array too?
– Jonathon
Nov 23 '18 at 23:53
Can you show the output in your question, either by adding a screenshot or pasting it in there? Do you get any errors or anything or is it just that you need an array? Do you need each item within the outer array to also be an array too?
– Jonathon
Nov 23 '18 at 23:53
It may also be worth mentioning that if you're outputting this as JSON, an associative array in PHP will be represented as an object in JSON. In Javascript/JSON an array is sequential and numerically indexed, an object allows for other types keys (but doesn't guarantee order).
– Jonathon
Nov 23 '18 at 23:55
It may also be worth mentioning that if you're outputting this as JSON, an associative array in PHP will be represented as an object in JSON. In Javascript/JSON an array is sequential and numerically indexed, an object allows for other types keys (but doesn't guarantee order).
– Jonathon
Nov 23 '18 at 23:55
add a comment |
2 Answers
2
active
oldest
votes
By reading your comment on other answer, I realized what you mean.
Hi @HCK, thanks for the answer. The
->toArray()
method doesn't seem to work and still returns it like{ "category_1": , "category_2": }
whereas I need it to do["category_1": , "category_2": ]
First, this answer is based on a guess that you are doing something like this on your controller (you didn't posted the controller code):
return reponse()->json($theResponseFromGetFeaturesMethod);
Since inside php the $theResponseFromGetFeaturesMethod
variable contains an dictionary array (something like ["key"=>"value]
), when you convert it to a JSON, you will notice that this "conversion" happens.
This happens because Javascript arrays (and JSON) doesn't support dictionary arrays. See this sample on javascript console:
> var a = ;
undefined
> a['key'] = "value"
"value"
> a
> key: "value"
length: 0
__proto__: Array(0)
Note that the a
still have a length of zero, but it now have an key
property.
This happens because almost everything on javascript is actually an object. So the array is a special kind of object that have push
, pop
and many other array methods. Doing array = 'somevalue'
is actually a shortcut to array.push('somevalue')
.
So, the behavior that you are observing is right, the toArray()
method work as expected, and the JSON conversion too.
Another weird behavior is when you try to convert this PHP array to an JSON:
[
0 => 'a'
1 => 'b'
9 => 'c'
]
You will note that in this case, PHP will convert this array to an object too. The result in JSON will be:
{
"0": "a",
"1": "b",
"2": "c"
}
This is also the expected behavior, since the JSON syntax doesn't support defining the index for a value.
Hmm, the only thing is I need to map this response in React. I am really trying to avoid having to do something likeObject.keys
do you have any suggestions?
– connormiotk96
Nov 23 '18 at 23:24
You can make your response looks something like:[ {"category": 1, "items": [...]}, {"category": 2, "items": [...]} ]
so you can use map (or filter, if you need a specific category) on this response
– Elias Soares
Nov 23 '18 at 23:32
add a comment |
When doing a query to get (possibly) several items using Eloquent, Laravel will always return an instance of the Collection class that contains all the model objects. If you need them converted to array to use them in a view you could compact
the elements. compact
will make an associative array of the elements of the collection:
public function getFeatures($id)
{
$features = Feature::all();
return view('my_cool_view', compact($features));
}
On the other hand, if you need them converted to array to return them through an API, Laravel convert the response to JSON by default:
public function getFeatures($id)
{
return Feature::all();
}
Now, if you somehow need the collection converted to an array, just use toArray()
like you indicated:
public function getFeatures($id)
{
$collection_of_features = Feature::all();
$array_of_features = $collection_of_features->toArray();
// use it for wherever you want.
}
Hi @HCK, thanks for the answer. The->toArray()
method doesn't seem to work and still returns it like{ "category_1": , "category_2": }
whereas I need it to do["category_1": , "category_2": ]
– connormiotk96
Nov 23 '18 at 22:02
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
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%2f53452861%2flaravel-groupby-returning-as-object%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
By reading your comment on other answer, I realized what you mean.
Hi @HCK, thanks for the answer. The
->toArray()
method doesn't seem to work and still returns it like{ "category_1": , "category_2": }
whereas I need it to do["category_1": , "category_2": ]
First, this answer is based on a guess that you are doing something like this on your controller (you didn't posted the controller code):
return reponse()->json($theResponseFromGetFeaturesMethod);
Since inside php the $theResponseFromGetFeaturesMethod
variable contains an dictionary array (something like ["key"=>"value]
), when you convert it to a JSON, you will notice that this "conversion" happens.
This happens because Javascript arrays (and JSON) doesn't support dictionary arrays. See this sample on javascript console:
> var a = ;
undefined
> a['key'] = "value"
"value"
> a
> key: "value"
length: 0
__proto__: Array(0)
Note that the a
still have a length of zero, but it now have an key
property.
This happens because almost everything on javascript is actually an object. So the array is a special kind of object that have push
, pop
and many other array methods. Doing array = 'somevalue'
is actually a shortcut to array.push('somevalue')
.
So, the behavior that you are observing is right, the toArray()
method work as expected, and the JSON conversion too.
Another weird behavior is when you try to convert this PHP array to an JSON:
[
0 => 'a'
1 => 'b'
9 => 'c'
]
You will note that in this case, PHP will convert this array to an object too. The result in JSON will be:
{
"0": "a",
"1": "b",
"2": "c"
}
This is also the expected behavior, since the JSON syntax doesn't support defining the index for a value.
Hmm, the only thing is I need to map this response in React. I am really trying to avoid having to do something likeObject.keys
do you have any suggestions?
– connormiotk96
Nov 23 '18 at 23:24
You can make your response looks something like:[ {"category": 1, "items": [...]}, {"category": 2, "items": [...]} ]
so you can use map (or filter, if you need a specific category) on this response
– Elias Soares
Nov 23 '18 at 23:32
add a comment |
By reading your comment on other answer, I realized what you mean.
Hi @HCK, thanks for the answer. The
->toArray()
method doesn't seem to work and still returns it like{ "category_1": , "category_2": }
whereas I need it to do["category_1": , "category_2": ]
First, this answer is based on a guess that you are doing something like this on your controller (you didn't posted the controller code):
return reponse()->json($theResponseFromGetFeaturesMethod);
Since inside php the $theResponseFromGetFeaturesMethod
variable contains an dictionary array (something like ["key"=>"value]
), when you convert it to a JSON, you will notice that this "conversion" happens.
This happens because Javascript arrays (and JSON) doesn't support dictionary arrays. See this sample on javascript console:
> var a = ;
undefined
> a['key'] = "value"
"value"
> a
> key: "value"
length: 0
__proto__: Array(0)
Note that the a
still have a length of zero, but it now have an key
property.
This happens because almost everything on javascript is actually an object. So the array is a special kind of object that have push
, pop
and many other array methods. Doing array = 'somevalue'
is actually a shortcut to array.push('somevalue')
.
So, the behavior that you are observing is right, the toArray()
method work as expected, and the JSON conversion too.
Another weird behavior is when you try to convert this PHP array to an JSON:
[
0 => 'a'
1 => 'b'
9 => 'c'
]
You will note that in this case, PHP will convert this array to an object too. The result in JSON will be:
{
"0": "a",
"1": "b",
"2": "c"
}
This is also the expected behavior, since the JSON syntax doesn't support defining the index for a value.
Hmm, the only thing is I need to map this response in React. I am really trying to avoid having to do something likeObject.keys
do you have any suggestions?
– connormiotk96
Nov 23 '18 at 23:24
You can make your response looks something like:[ {"category": 1, "items": [...]}, {"category": 2, "items": [...]} ]
so you can use map (or filter, if you need a specific category) on this response
– Elias Soares
Nov 23 '18 at 23:32
add a comment |
By reading your comment on other answer, I realized what you mean.
Hi @HCK, thanks for the answer. The
->toArray()
method doesn't seem to work and still returns it like{ "category_1": , "category_2": }
whereas I need it to do["category_1": , "category_2": ]
First, this answer is based on a guess that you are doing something like this on your controller (you didn't posted the controller code):
return reponse()->json($theResponseFromGetFeaturesMethod);
Since inside php the $theResponseFromGetFeaturesMethod
variable contains an dictionary array (something like ["key"=>"value]
), when you convert it to a JSON, you will notice that this "conversion" happens.
This happens because Javascript arrays (and JSON) doesn't support dictionary arrays. See this sample on javascript console:
> var a = ;
undefined
> a['key'] = "value"
"value"
> a
> key: "value"
length: 0
__proto__: Array(0)
Note that the a
still have a length of zero, but it now have an key
property.
This happens because almost everything on javascript is actually an object. So the array is a special kind of object that have push
, pop
and many other array methods. Doing array = 'somevalue'
is actually a shortcut to array.push('somevalue')
.
So, the behavior that you are observing is right, the toArray()
method work as expected, and the JSON conversion too.
Another weird behavior is when you try to convert this PHP array to an JSON:
[
0 => 'a'
1 => 'b'
9 => 'c'
]
You will note that in this case, PHP will convert this array to an object too. The result in JSON will be:
{
"0": "a",
"1": "b",
"2": "c"
}
This is also the expected behavior, since the JSON syntax doesn't support defining the index for a value.
By reading your comment on other answer, I realized what you mean.
Hi @HCK, thanks for the answer. The
->toArray()
method doesn't seem to work and still returns it like{ "category_1": , "category_2": }
whereas I need it to do["category_1": , "category_2": ]
First, this answer is based on a guess that you are doing something like this on your controller (you didn't posted the controller code):
return reponse()->json($theResponseFromGetFeaturesMethod);
Since inside php the $theResponseFromGetFeaturesMethod
variable contains an dictionary array (something like ["key"=>"value]
), when you convert it to a JSON, you will notice that this "conversion" happens.
This happens because Javascript arrays (and JSON) doesn't support dictionary arrays. See this sample on javascript console:
> var a = ;
undefined
> a['key'] = "value"
"value"
> a
> key: "value"
length: 0
__proto__: Array(0)
Note that the a
still have a length of zero, but it now have an key
property.
This happens because almost everything on javascript is actually an object. So the array is a special kind of object that have push
, pop
and many other array methods. Doing array = 'somevalue'
is actually a shortcut to array.push('somevalue')
.
So, the behavior that you are observing is right, the toArray()
method work as expected, and the JSON conversion too.
Another weird behavior is when you try to convert this PHP array to an JSON:
[
0 => 'a'
1 => 'b'
9 => 'c'
]
You will note that in this case, PHP will convert this array to an object too. The result in JSON will be:
{
"0": "a",
"1": "b",
"2": "c"
}
This is also the expected behavior, since the JSON syntax doesn't support defining the index for a value.
answered Nov 23 '18 at 23:22
Elias SoaresElias Soares
2,94811337
2,94811337
Hmm, the only thing is I need to map this response in React. I am really trying to avoid having to do something likeObject.keys
do you have any suggestions?
– connormiotk96
Nov 23 '18 at 23:24
You can make your response looks something like:[ {"category": 1, "items": [...]}, {"category": 2, "items": [...]} ]
so you can use map (or filter, if you need a specific category) on this response
– Elias Soares
Nov 23 '18 at 23:32
add a comment |
Hmm, the only thing is I need to map this response in React. I am really trying to avoid having to do something likeObject.keys
do you have any suggestions?
– connormiotk96
Nov 23 '18 at 23:24
You can make your response looks something like:[ {"category": 1, "items": [...]}, {"category": 2, "items": [...]} ]
so you can use map (or filter, if you need a specific category) on this response
– Elias Soares
Nov 23 '18 at 23:32
Hmm, the only thing is I need to map this response in React. I am really trying to avoid having to do something like
Object.keys
do you have any suggestions?– connormiotk96
Nov 23 '18 at 23:24
Hmm, the only thing is I need to map this response in React. I am really trying to avoid having to do something like
Object.keys
do you have any suggestions?– connormiotk96
Nov 23 '18 at 23:24
You can make your response looks something like:
[ {"category": 1, "items": [...]}, {"category": 2, "items": [...]} ]
so you can use map (or filter, if you need a specific category) on this response– Elias Soares
Nov 23 '18 at 23:32
You can make your response looks something like:
[ {"category": 1, "items": [...]}, {"category": 2, "items": [...]} ]
so you can use map (or filter, if you need a specific category) on this response– Elias Soares
Nov 23 '18 at 23:32
add a comment |
When doing a query to get (possibly) several items using Eloquent, Laravel will always return an instance of the Collection class that contains all the model objects. If you need them converted to array to use them in a view you could compact
the elements. compact
will make an associative array of the elements of the collection:
public function getFeatures($id)
{
$features = Feature::all();
return view('my_cool_view', compact($features));
}
On the other hand, if you need them converted to array to return them through an API, Laravel convert the response to JSON by default:
public function getFeatures($id)
{
return Feature::all();
}
Now, if you somehow need the collection converted to an array, just use toArray()
like you indicated:
public function getFeatures($id)
{
$collection_of_features = Feature::all();
$array_of_features = $collection_of_features->toArray();
// use it for wherever you want.
}
Hi @HCK, thanks for the answer. The->toArray()
method doesn't seem to work and still returns it like{ "category_1": , "category_2": }
whereas I need it to do["category_1": , "category_2": ]
– connormiotk96
Nov 23 '18 at 22:02
add a comment |
When doing a query to get (possibly) several items using Eloquent, Laravel will always return an instance of the Collection class that contains all the model objects. If you need them converted to array to use them in a view you could compact
the elements. compact
will make an associative array of the elements of the collection:
public function getFeatures($id)
{
$features = Feature::all();
return view('my_cool_view', compact($features));
}
On the other hand, if you need them converted to array to return them through an API, Laravel convert the response to JSON by default:
public function getFeatures($id)
{
return Feature::all();
}
Now, if you somehow need the collection converted to an array, just use toArray()
like you indicated:
public function getFeatures($id)
{
$collection_of_features = Feature::all();
$array_of_features = $collection_of_features->toArray();
// use it for wherever you want.
}
Hi @HCK, thanks for the answer. The->toArray()
method doesn't seem to work and still returns it like{ "category_1": , "category_2": }
whereas I need it to do["category_1": , "category_2": ]
– connormiotk96
Nov 23 '18 at 22:02
add a comment |
When doing a query to get (possibly) several items using Eloquent, Laravel will always return an instance of the Collection class that contains all the model objects. If you need them converted to array to use them in a view you could compact
the elements. compact
will make an associative array of the elements of the collection:
public function getFeatures($id)
{
$features = Feature::all();
return view('my_cool_view', compact($features));
}
On the other hand, if you need them converted to array to return them through an API, Laravel convert the response to JSON by default:
public function getFeatures($id)
{
return Feature::all();
}
Now, if you somehow need the collection converted to an array, just use toArray()
like you indicated:
public function getFeatures($id)
{
$collection_of_features = Feature::all();
$array_of_features = $collection_of_features->toArray();
// use it for wherever you want.
}
When doing a query to get (possibly) several items using Eloquent, Laravel will always return an instance of the Collection class that contains all the model objects. If you need them converted to array to use them in a view you could compact
the elements. compact
will make an associative array of the elements of the collection:
public function getFeatures($id)
{
$features = Feature::all();
return view('my_cool_view', compact($features));
}
On the other hand, if you need them converted to array to return them through an API, Laravel convert the response to JSON by default:
public function getFeatures($id)
{
return Feature::all();
}
Now, if you somehow need the collection converted to an array, just use toArray()
like you indicated:
public function getFeatures($id)
{
$collection_of_features = Feature::all();
$array_of_features = $collection_of_features->toArray();
// use it for wherever you want.
}
answered Nov 23 '18 at 21:24
HCKHCK
3,81011338
3,81011338
Hi @HCK, thanks for the answer. The->toArray()
method doesn't seem to work and still returns it like{ "category_1": , "category_2": }
whereas I need it to do["category_1": , "category_2": ]
– connormiotk96
Nov 23 '18 at 22:02
add a comment |
Hi @HCK, thanks for the answer. The->toArray()
method doesn't seem to work and still returns it like{ "category_1": , "category_2": }
whereas I need it to do["category_1": , "category_2": ]
– connormiotk96
Nov 23 '18 at 22:02
Hi @HCK, thanks for the answer. The
->toArray()
method doesn't seem to work and still returns it like { "category_1": , "category_2": }
whereas I need it to do ["category_1": , "category_2": ]
– connormiotk96
Nov 23 '18 at 22:02
Hi @HCK, thanks for the answer. The
->toArray()
method doesn't seem to work and still returns it like { "category_1": , "category_2": }
whereas I need it to do ["category_1": , "category_2": ]
– connormiotk96
Nov 23 '18 at 22:02
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
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%2f53452861%2flaravel-groupby-returning-as-object%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
What is your use case? Do you need them converted to array to return it to a view or through an API?
– HCK
Nov 23 '18 at 21:16
Can you add a
dd(...)
of your output?– Jonathon
Nov 23 '18 at 22:50
@Jonathon the
dd()
actually says that it is an array. Does the response convert to an object by default?– connormiotk96
Nov 23 '18 at 23:07
Can you show the output in your question, either by adding a screenshot or pasting it in there? Do you get any errors or anything or is it just that you need an array? Do you need each item within the outer array to also be an array too?
– Jonathon
Nov 23 '18 at 23:53
It may also be worth mentioning that if you're outputting this as JSON, an associative array in PHP will be represented as an object in JSON. In Javascript/JSON an array is sequential and numerically indexed, an object allows for other types keys (but doesn't guarantee order).
– Jonathon
Nov 23 '18 at 23:55