You can convert collections in array and convert eloquent model to json.
Sometime you need array instead of collections then you can use toArray
method to convert collections to array.If you want to convert model to json then use toJson
method.
You can also cast a collection or model to string.
here is sample code to convert collections object to array in Laravel.
- $products = App\Product::all();
- return $products->toArray();
- $product = App\Product::find(1);
- return $product->toJson();
- $product = App\Product::find(1);
- return (string) $product;