Level 12
Ok I found some help! I leave this post be, maybe it's helpfull as well:
abstract class Recommendation implements Arrayable, JsonSerializable {
}
...
class SomeRecomendation extends Recommendation {
public function jsonSerialize()
{
return $this->toArray();
}
public function toArray(){
return [
'title' => $title,
'description' => $description,
'courses' => (collection) $courses
];
}
}
Now watch will happen, When you return ['collecion' => new SomeRecomendation()], Laravel calls json_encode on it ( to return response as json ), Because new SomeRecomendation() is instance of JsonSerializable it will call jsonSerialize method and encodes an array returned from it.