【问题标题】:Laravel both of created_at and updated_at update to same value in update actionLaravel created_at 和 updated_at 在更新操作中都更新为相同的值
【发布时间】:2016-04-17 16:47:42
【问题描述】:

在 laravel 中使用其他数据库的列信息更新现有数据后,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();
    });
}

【问题讨论】:

  • 如果你使用迁移,你能展示你是如何创建表的吗?
  • @Abhishek 是的,帖子已更新,请查看。

标签: php laravel


【解决方案1】:

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

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

你可以试试这个,可能会奏效

【讨论】:

    【解决方案2】:

    这不是 Laravel 或 PHP 的问题,而是 MySQL 5.7 的变化(您可能使用 MySQL 5.7)。为了解决这个问题,你需要允许时间戳为空,这样 MySQL 就不会用当前时间戳填充那些,所以在 Laravel 中你需要使用 nullableTimestamps() 而不是 timestamps() 或使用 timestamp()-&gt;nullable() 作为单个时间戳字段。

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-08-15
        • 2016-04-05
        • 2014-08-17
        • 2013-10-05
        • 2016-04-12
        • 2015-10-26
        相关资源
        最近更新 更多