added NeoEloquentFactory class that will be replacing default Laravel's Factory class;
added NeoFactoryBuilder class that extends default Laravel's Factory class;
added alias to replace default Illuminate\Database\Eloquent\Factory with a NeoEloquentFactory;
updated readme.
Now it will be possible to generate Eloquent models(factory(xxx)->make()/ factory(xxx)->create()) via default Laravel's factory() helper function.
Issue details:
Previously when we tried to factory(FooModel:class)->create(), we had next issues:
Wrong check for Model(abstract class Illuminate\Database\Eloquent\Model) instance that is used in default Illuminate\Database\Eloquent\FactoryBuilder.php thus when we try to create 1 model, it thinks that we try to create a collection of models and we get the each() method does not exists... error.
/**
* Create a collection of models and persist them to the database.
*
* @param array $attributes
* @return mixed
*/
public function create(array $attributes = [])
{
$results = $this->make($attributes);
if ($results instanceof Model) {
$this->store(collect([$results]));
} else {
$this->store($results);
}
return $results;
}
Issue in default Illuminate\Database\Eloquent\FactoryBuilder.php (store() method) about non-existing getName() method...
Was:
protected function store($results)
{
$results->each(function ($model) {
if (! isset($this->connection)) {
$model->setConnection($model->newQueryWithoutScopes()->getConnection()->getName());
}
$model->save();
});
}
will be:
/**
* Set the connection name on the results and store them.
*
* @param \Illuminate\Support\Collection $results
* @return void
*/
protected function store($results)
{
$results->each(function ($model) {
if (! isset($this->connection)) {
$model->setConnection($model->getConnectionName());
}
$model->save();
});
}
NeoEloquentFactory
class that will be replacing default Laravel'sFactory
class;Factory
class;Illuminate\Database\Eloquent\Factory
with aNeoEloquentFactory
;readme
.Now it will be possible to generate Eloquent models(
factory(xxx)->make()
/factory(xxx)->create()
) via default Laravel'sfactory()
helper function.Issue details: Previously when we tried to
factory(FooModel:class)->create()
, we had next issues:Wrong check for Model(
abstract class Illuminate\Database\Eloquent\Model
) instance that is used in defaultIlluminate\Database\Eloquent\FactoryBuilder.php
thus when we try to create 1 model, it thinks that we try to create a collection of models and we get theeach() method does not exists...
error.Issue in default
Illuminate\Database\Eloquent\FactoryBuilder.php
(store()
method) aboutnon-existing getName() method...
Was:will be: