Argument '1' passed to set() is expected to be of type App\Models\YourModel, Illuminate\Database\Eloquent\Collection givenPHP(PHP0406)
when using YourModel::find($id) as a parameter this error is shown, when the parameter must be of type YourModel
Argument '1' passed to set() is expected to be of type App\Models\YourModel, Illuminate\Database\Eloquent\Collection givenPHP(PHP0406)
when using YourModel::find($id) as a parameter this error is shown, when the parameter must be of type YourModel
find may accept an array in which case it gives a collection.
I think if the type system fails to infer the type passed in it will do this.
Lets say you have:
$id = $someInputArray['id'];
Model::find($id); // If type of ['id'] is unknown it will say its a collection.
You can try to fix it by casting it to an int if you are sure it is one:
$id = (int) $someInputArray['id'];
Model::find($id); // Should be Model here
akamasAT Thank you for reporting the issue.
May I ask for more details? As I'm trying to find a test case, it works as expected:
We have improved the "magic" return type of Model::find($id);
where $id
is int
. (Will be available in the next update)
@akamasAT Since there are a lot of very specific functions in Laravel/Eloquent, may I ask for more details? What was the complete expression you're having troubles with. What set()
method are you referring to?