When you override an Eloquent Model with public static function create
and have it call parent::create, you are getting a never ending loop because it sends it to line 1326 __callStatic
which instantiates a new version of the Model and calls create on it. Because the create method was removed from Model, this just calls your create method again.
It appears that the create method was moved to the Builder class (I can't see any documentation in any release notes) and this causes a breaking change when upgrading to 5.4. Is there a new way to do this?
public static function create()
The only option I can see is to modify my query to do (new static)->newQuery()->create($attributes);
This seems a little counter intuitive.
Copying from the upgrade guide https://laravel.com/docs/5.4/upgrade
The Model::create & Model:: forceCreate methods have been moved to the IlluminateDatabaseEloquentBuilder class in order to provide better support for creating models on multiple connections. However, if you are extending these methods in your own models, you will need to modify your implementation to call the create method on the builder. For example:
public static function create(array $attributes = [])
{
$model = static::query()->create($attributes);
// ...
return $model;
}
@themsaid I can't believe I didn't see that. Thanks!
welcome :) It was only added a few days ago.
Wow did I need this today! Thanks!
Most helpful comment
Copying from the upgrade guide https://laravel.com/docs/5.4/upgrade
The create & forceCreate Methods
The Model::create & Model:: forceCreate methods have been moved to the IlluminateDatabaseEloquentBuilder class in order to provide better support for creating models on multiple connections. However, if you are extending these methods in your own models, you will need to modify your implementation to call the create method on the builder. For example: