app\models\EntityModel::createNew PHP Method

createNew() public static method

public static createNew ( null $context = null ) : mixed
$context null
return mixed
    public static function createNew($context = null)
    {
        $className = get_called_class();
        $entity = new $className();
        if ($context) {
            $user = $context instanceof User ? $context : $context->user;
            $account = $context->account;
        } elseif (Auth::check()) {
            $user = Auth::user();
            $account = Auth::user()->account;
        } else {
            Utils::fatalError();
        }
        $entity->user_id = $user->id;
        $entity->account_id = $account->id;
        // store references to the original user/account to prevent needing to reload them
        $entity->setRelation('user', $user);
        $entity->setRelation('account', $account);
        if (method_exists($className, 'trashed')) {
            $lastEntity = $className::whereAccountId($entity->account_id)->withTrashed();
        } else {
            $lastEntity = $className::whereAccountId($entity->account_id);
        }
        if (static::$hasPublicId) {
            $lastEntity = $lastEntity->orderBy('public_id', 'DESC')->first();
            if ($lastEntity) {
                $entity->public_id = $lastEntity->public_id + 1;
            } else {
                $entity->public_id = 1;
            }
        }
        return $entity;
    }