Skip to content
Advertisement

How to group array by key in PHP Laravel Builder query

I am new Laravel and I just want to group my array by key. Here’s what I have done so far:

Code:

JavaScript

Return Value:

JavaScript

I just want something like this:

JavaScript

I tried foreach to $vehicles variable but it says Cannot use object of type stdClass as array is there any way to achieve this? thanks in advance. Have a good day~

Advertisement

Answer

Considering you code, I would use Laravel Collections, to do something like that:

JavaScript

Notice, that the error you got its not about iterating in foreach it’s because you are returning a array of stdClass.

JavaScript

Back to you problem in Laravel, I recommend you to debug using dump or dd (dump and die) Laravel’s functions.

JavaScript
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement