2016-05-31 9 views
2

で更新する私は、3台の台車、薬局やcart_pharmaciesupdated_atの列雄弁

カートテーブル

cart_id | user_id | total_price | status | created_at | updated_at 

薬局テーブル

pharmacy_id | name | address_id | created_at | updated_at 

cart_pharmaciesテーブル

を持っていながら、 01カートに
cart_pharmacies_id | cart_id | pharmacy_id | created_at | updated_at 

私は私が私がコード

$pharmacy_id=$request->input('pharmacy_id'); 
$pharmacy= Pharmacy::findOrFail($pharmacy_id); 
$pharmacy->cart()->update(['status'=>1]); 
でカートのステータスを更新しようとしていますpharmacy_idているコントローラで

public function cart() 
{ 
    return $this->belongsToMany('App\Cart','cart_pharmacies','pharmacy_id','cart_id'); 
} 

を定義モーダルparmacyに関係

public function pharmacy() 
    { 
     return $this->belongsToMany('App\Pharmacy','cart_pharmacies','cart_id','pharmacy_id'); 
    } 

を定義モーダル

しかし、それは私にエラーを与えている

SQLSTATE[23000]: Integrity constraint violation: 1052 Column 
'updated_at' in field list is ambiguous (SQL: update `cart` inner join 
`cart_pharmacies` on `cart`.`cart_id` = `cart_pharmacies`.`cart_id` set 
`status` = 1, 
`updated_at` = 2016-05-31 07:14:47 where `cart_pharmacies`.`pharmacy_id` = 5) 
+1

どのように値updated_atの渡していますか? –

+0

私のコードで私はupdated_atの値を渡していません。 –

+0

これはupdated_atの値の周りに小さな引用符がないという問題です。値を渡している共通のコードがあるようです。それを見てみましょう。 –

答えて

1

SQLクエリのエラーがで、「updated_at」(メインおよび関連の両方のテーブル)とカントを更新するかを決めるという名前の複数の列があると言います。これはバグであり、私はupdated_atが指定されたテーブル名なしで自動的に最後に追加されるため、雄弁で解決する方法がないと思います。あなたはモーダル使用する場合

+1

私はこれに同意します – Jonathan

0

このライン

public $timestamps = false;

試してみると、あなたのモーダルを更新してください、それが役に立つかもしれあなたが自動的に雄弁で制御することができ、あなたのピボットテーブルにタイムスタンプを使用している

+0

これは動作していますが、updated_atの値は格納されません –

+1

は、TIMESTAMPのデフォルト値をCURRENT_TIMESTAMPで設定しています。あなたのデータベースに – Chamandeep

0

withTimestamps()メソッド。あなたのparmacyモーダルで

パブリック関数カート(){

return $this->belongsToMany('App\Cart','cart_pharmacies','pharmacy_id','cart_id')->withTimestamps(); 

}