只有用户模型使用空值保存到数据库

Vir*_*ani 11 php laravel eloquent laravel-5.4

在Laravel 5.4中,当我尝试将用户模型保存到数据库时,不保存值.我也设置了可填充属性.

它在Laravel 5.3中工作.将应用程序升级到Laravel 5.4之后,就会出现此问题.

以下是用户模型.

class User extends BaseModel implements AuthenticatableContract, CanResetPasswordContract, JWTSubject
{
    use SoftDeletes,
        UserAccess,
        UserAttribute,
        UserRelationship,
        Authenticatable,
        CanResetPassword,
        Notifiable;

    /**
     * Database Table
     *
     * @var string
     */
    protected $table = "users";

    /**
     * The attributes that are not mass assignable.
     *
     * @var array
     */
    protected $guarded = ['id'];

    /**
     * Fillable Form Fields
     *
     * @var array
     */
    protected $fillable = [
        'name',
        'first_name',
        'last_name',
        'email',
        'password',
        'status',
        'confirmed',
        'api_user',
        'confirmation_code',
        'account_id',
        'role_id',
        'cw_contact_id',
        'all',
        'all_locations',
        'username',
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = ['password', 'remember_token'];

    /**
     * Select HTML Preference
     *
     * @var string
     */
    protected static $selectHTMLFormat = "[email]";

    /**
     * @var array
     */
    protected $dates = ['deleted_at', 'last_login'];
}
Run Code Online (Sandbox Code Playgroud)

请注意,问题仅在于用户模型.

我正在保存用户如下.

// Create User
        $user = $this->model->create([
            'first_name'        => $input['first_name'],
            'last_name'         => $input['last_name'],
            'username'          => $input['username'],
            'email'             => $input['email'],
            'password'          => bcrypt($input['password']),
            'confirmation_code' => md5(uniqid(mt_rand(), true)),
            'confirmed'         => 1,
            'api_user'          => (isset($input['api_user']) ? $input['api_user'] : 0),
            'account_id'        => $input['account_id'],
            'role_id'           => (isset($input['role_id']) ? $input['role_id'] : 0),
            'all'               => (!isset($input['associated-permissions']) || $input['associated-permissions'] == 'all') ? 1 : 0,
            'status'            => (!isset($input['status']) || $input['status'] ? 1 : 0),
            'all_locations'     => $input['all_locations']
        ]);
Run Code Online (Sandbox Code Playgroud)

然后将调用BaseModel的create方法,下面是它的代码.

public static function create(array $attributes = Array())
{
    $user = access()->user();

    if($user)
    {
        $attributes['account_id'] = (!isset($attributes['account_id']) ? $user->account->id : $attributes['account_id'] );
    }

    $childClass     = get_called_class();
    $model          = new $childClass;
    $model->runActionLogger(false, 'create');

    return parent::query()->create($attributes);
}
Run Code Online (Sandbox Code Playgroud)

Dim*_*rey 7

原因很可能是Laravel 5.4中的新中间件,称为"Request Sanitization Middleware",如https://laravel.com/docs/5.4/releases中所述.

\Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class,在app/Http/kernel.php中禁用,看看你得到了什么.

您还可以检入/config/database.php和您的mysql连接设置:'strict' => true如果是,请设置为false.

一个好的做法是使用该模型进行用户输入.在这种情况下,不要用$user = $this->model->create(...)你填充模型 $user = new \App\User($input)并从那里更新你的值,而不是f.ex. $user->confirmation_code = md5(uniqid(mt_rand(), true));$user->password = bcrypt($user->password);

如果字段可以为空,请在迁移文件中指明为f.ex. $table->string('all')->nullable();

如果完成就跑了 $user->save();


dpa*_*oli 5

从5.4开始,create()函数的定义不再Illuminate\Database\Eloquent\Model如下:

被处理为dinamic方法调用,即通过调用这些函数之一(依赖于它是否静态调用):

public static function __callStatic($method, $parameters)
// or
public function __call($method, $parameters)
Run Code Online (Sandbox Code Playgroud)

Illuminate\Database\Eloquent\Model课堂上.

现在我没有你所有的代码,但是,恕我直言,我会尝试在你的BaseModel类中改变这一行:

return parent::query()->create($attributes);
Run Code Online (Sandbox Code Playgroud)

对此:

return $model->create($attributes);
Run Code Online (Sandbox Code Playgroud)

或者,对我来说更好,对此:

return (new static)->newQuery()->create($attributes);
Run Code Online (Sandbox Code Playgroud)


Vir*_*ani 1

伙计们,我可以使用 Fill() 方法解决问题。

public static function create(array $attributes = Array())
{
    $user = access()->user();

    if($user)
    {
        $attributes['account_id'] = (!isset($attributes['account_id']) ? $user->account->id : $attributes['account_id'] );
    }

    $childClass     = get_called_class();
    $model          = new $childClass;

    $model->fill($attributes);
    $model->save();

    $model->runActionLogger($model, 'create');

    return $model;
}
Run Code Online (Sandbox Code Playgroud)

另外,我错误地添加了 CanResetPassword Trait 的构造,这也导致了问题。所以如果我删除它,一切都会像以前一样工作。