Gesparo
10/20/2018 - 5:17 PM

Syntax suger for factory function

In Laravel we have function factory(Model, $amount)->create($attr) and the same for update. It is more readable wrapper for it.

Run: "composer dumpautoload" command after adding this files.

{
  "autoload-dev": {
        "files": ["path_to_functions/factory_create_wrapper.php"],
        "files": ["path_to_functions/factory_update_wrapper.php"]
  },
}
use Illuminate\Database\Eloquent\Model;

/**
 * @param $class
 * @param array $attributes
 * @param null $amount
 * @return Model
 */
function make($class, $attributes = [], $amount = null)
{
    return factory($class, $amount)->make($attributes);
}
use Illuminate\Database\Eloquent\Model;

/**
 * @param $class
 * @param array $attributes
 * @param null $amount
 * @return Model
 */
function create($class, $attributes = [], $amount = null)
{
    return factory($class, $amount)->create($attributes);
}