mopo922/LaravelTreats

Collection method first throws an exception when using HasCompositePrimaryKey

sk33wiff opened this issue · 1 comments

Collection method first throws an exception when using HasCompositePrimaryKey:

$sources = $sources->first(function($model) { return $model->getKey() === 1; });

[ErrorException]
array_key_exists(): The first argument should be either a string or an integer

Exception trace:
() at /git/api/src/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php:311
Illuminate\Foundation\Bootstrap\HandleExceptions->handleError() at n/a:n/a
array_key_exists() at /git/api/src/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php:311
Illuminate\Database\Eloquent\Model->getAttribute() at /git/api/src/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:1265
Illuminate\Database\Eloquent\Model->getKey() at /git/api/src/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Collection.php:34
Illuminate\Database\Eloquent\Collection->Illuminate\Database\Eloquent{closure}() at n/a:n/a
call_user_func() at /git/api/src/vendor/laravel/framework/src/Illuminate/Support/Arr.php:175
Illuminate\Support\Arr::first() at /git/api/src/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Collection.php:35

My bad, sorry.

I got confused with Lavs api, because while this is possible:

$sources = $sources->first();

this isn't:

$sources = $sources->first(function($model) { return $model->getKey() === 1; });

however, this is:

$sources = $sources->all()->first(function($model) { return $model->getKey() === 1; });