I have a laravel application where the route "/phones/brand/{brand_id}" returns all the phones associated with that brand. This works perfectly except with brands other than brand_id 1, the output is different.
/phones/brand/1 result
[{"id":1,"phone_model_id":1,"model":"Iphone 5"},
{"id":2,"phone_model_id":1,"model":"Iphone 5s"},
{"id":3,"phone_model_id":1,"model":"Iphone 6"}]
/phones/brand/2 result
{"3":{"id":6,"phone_model_id":2,"model":"galaxyy s3"}}
I am unable to work with this data as it returns empty slots in javascript.
Console log:
Object [ <3 empty slots>, Object ] script.js:84:11
Here is also the code for the controller action for that endpoint.
public function showByBrand($brand)
{
return Phone::all()->where('phone_model_id', '=', $brand);
}
Phone::where('phone_model_id', '=', $brand)->get(['id', 'model', 'phone_model_id'])->toJson();?