Laravel日期未插入,在数据库表中更新

时间:2015-08-21 15:44:13

标签: php mysql date laravel laravel-5

插入并编辑注册表但没有日期列。 |

编程看似很好,但未能正确注册日期字段。

使用UTF8编码,语言环境为(Es)

为什么呢? T_T

表格

Schema::create('products', function(Blueprint $table)
    {
        $table->increments('id');
        $table->integer('subclassproduct_id')->unsigned();
        $table->integer('branch_id')->unsigned();
        $table->integer('coin_id')->unsigned();
        $table->string('code', 50);
        $table->string('name', 100);
        $table->string('description', 200);
        $table->text('observation');
        $table->decimal('price', 5,2);
        $table->date('validity_since');
        $table->date('validity_until');
        $table->enum('state', ['Activo', 'Desactivado']); 
        $table->softDeletes();
        $table->timestamps();

        $table->foreign('subclassproduct_id')
            ->references('id')->on('subclasses_products')
            ->onUpdate('CASCADE')
            ->onDelete('CASCADE');  

        $table->foreign('branch_id')
            ->references('id')->on('branches')
            ->onUpdate('CASCADE')
            ->onDelete('CASCADE');

        $table->foreign('coin_id')
            ->references('id')->on('coins')
            ->onUpdate('CASCADE')
            ->onDelete('CASCADE');
    });

模型

这是表格的模型

class Product extends Model {
use  SoftDeletes; // for soft delete

protected $table = 'products';

protected $fillable = [
    'subclassproduct_id', 'branch_id', 'coin_id', 'code' ,'name', 'description', 'observation', 'price', 'validity_since', 'validity_until', 'state'
];

protected $dates = ['deleted_at'];   }

控制器

这是表格的控制者

    public function update(EditProductRequest $request, $id)
{
    $Product = Product::findOrFail($id);
    $Product->fill($request->all());

    $request->merge(array('validity_since' => Carbon::createFromFormat('d/m/Y', $request->get('validity_since') )->toDateString()));
    $request->merge(array('validity_until' => Carbon::createFromFormat('d/m/Y', $request->get('validity_until') )->toDateString()));



    $Product->save();

    return redirect()->route('administrar.products.index');
}

1 个答案:

答案 0 :(得分:1)

fill之前移动这些行:

$request->merge(...);
$request->merge(...);

然后使用fill和其余代码:

$Product->fill($request->all());
$Product->save();

// refirect or whatever...

顺便说一句,您可以将单mergearray

一起使用
$request->merge([
    'validity_since' => 'value',
    'validity_until' => 'value'
]);