Laravel created_at和updated_at在更新操作中更新为相同的值


Laravel both of created_at and updated_at update to same value in update action

在用其他数据库的列信息更新现有数据后,created_atupdated_at都更新到相同的值。

我的表单:

{!! Form::model($user_data,array('url' => array('manageUsers', $user_data->id), 'files' => true,'method' => 'PUT', 'class'=>'validate','id'=>'xfrm_submit')) !!}
{!! Form::hidden('id',$user_data->id) !!}
<div class="form-group">
    {!! Form::label( 'acceptor_name','Acceptor Name:' ) !!}
    <span style="color:#ff0000;padding: 0 2px 2px 0">*</span>
    <input id="acceptor_name"
           name="acceptor_name"
           type="text"
           value="{{$user_data->acceptor_name}}">
</div>
</form>

我的控制器update操作:

public function update(StoreUserManagment $request)
    {
        $user_data  = User::find($request->input('id'));
        $user_data->acceptor_name =$request->input('acceptor_name');
        $user_data->save();
        return redirect('manageUsers');
    }

迁移文件:

public function up()
{
    Schema::create('users', function (Blueprint $table) {
        $table->increments('id');
        $table->string('name', '20');
        $table->string('family', '25');
        $table->string('username', '15')->unique();
        $table->string('password', '64');
        $table->string('token', '50');
        $table->string('email', '20')->unique();
        $table->string('remember_token', '100');
        $table->string('image_file_name', '50');
        $table->string('mobile_number', '13');
        $table->tinyInteger('status');
        $table->text('account_state_note');
        $table->timestamps();
    });
}

这是个问题https://github.com/laravel/framework/issues/11518

我使用->nullableTimestamps()而不是->timestamps() 解决了这个问题

你可以试试这个,也许它会工作

这不是Laravel或PHP的问题,但这是MySQL 5.7的更改(你可能使用MySQL 5.7)。要解决这个问题,你需要允许时间戳为null,这样MySQL就不会用当前的时间戳填充这些时间戳,所以在Laravel中,你需要使用nullableTimestamps()而不是timestamps(),或者使用timestamp()->nullable()作为单个时间戳字段。

您在控制器中调用save()而不是update()