Look at this example:
use App\Http\Resources\UserResource;
use App\Models\User;
Route::get('/user/{id}', function ($id) {
return new UserResource(User::findOrFail($id));
});
How does this internally work? Because at first glance we just return an Object of the class UserResource
. But Laravel is magically calling the toArray
function and resolves it correctly.
I want the same for my Recommendation class to work.
use App\Http\Recommendations\RecentlyUpdatedRecommendation;
use ...
Route::get('/someurl', function () {
return ['collections' => [
new RecentlyUpdatedRecommendation(),
new WatchlistRecommendation(),
new LastWatchedRecommendation(),
...
]];
});
With
/**
* All Recommendations use this as output
*/
abstract class Recommendation
{
// No Idea what needs to be put here
}
The output array/json should resolve into this:
return [
'title' => $title,
'description' => $description,
'courses' => (collection) $courses
];