I got a task and I need your help. Firstly I would like to show you my App\Repositories\xyz.php:
public function getCars(): Collection
{
return collect([
new car(...),
new car(...),
new car(...),
]);
}
This is a collection.
There is a lot of data in it. And my task is that I have to use a Http Get Api endpoint to show ONE random Car from these in JSON format. I made a new controller file for this and the route for it but I don't know how can I continue it.
Object of class App\Repositories\CarRepository could not be converted to string
This suggests that you are attempting to echo the repo somewhere? $randomCar should give you one instance of the car class in the Collection; just use the $repo in scope rather than an instance variable:
public function index()
{
$repo = new CarRepository();
$randomcar = $repo->getCars()->random();
dd($randomcar); // a car instance???
}