I assume that MatchList does not extend eloquent?
This is how eloquent does it. https://github.com/laravel/framework/blob/6.x/src/Illuminate/Database/Eloquent/Model.php#L1638
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
Hey,
I got an object like this:
MyApp\Match\MatchList {#78 ▼
-platformId: "EUW1"
-gameId: 4312601422
-championId: 21
-queueId: 420
-season: 13
-timestamp: 1575827032872
-role: "DUO_CARRY"
-lane: "BOTTOM"
}
But if I try to return it, I get an error like this:
The Response content must be a string or object implementing __toString(), "object" given.
This is how I get the object:
foreach ($matchList['matches'] as $game){
return new MatchList(
$game['platformId'],
$game['gameId'],
$game['champion'],
$game['queue'],
$game['season'],
$game['timestamp'],
$game['role'],
$game['lane'],
);
}
I tried to cast the object to string with (string), but it didnt work. I read about a __toString magic method, but I don't understand how I implement it in my MatchList class.
Well it would be quite easy to make some sot of __toString yourself
public function __toString()
{
$data = [
'platformId' => $this->platformId,
'gameId' => $this->gameId,
...
];
return json_encode($data);
}
Please or to participate in this conversation.