2015-08-21 34 views
1

插入並編輯註冊表,但沒有日期列。 |Laravel日期未插入,更新數據庫表

編程看似很好,但未能正確註冊日期字段。

使用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

移動這些線fill之前的控制器:

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

然後用fill和代碼的其餘部分:

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

// refirect or whatever... 

順便說一句,你可以使用單個mergearray

$request->merge([ 
    'validity_since' => 'value', 
    'validity_until' => 'value' 
]); 
+0

謝謝 我是如此不知所措,我沒看到 –

+0

最受歡迎。沒關係,它發生了:-) –