2017-09-08 58 views
0

我試圖更新訂單表,其具有通過鏈接的freign鍵約束CUSTOMER_ID到客戶表。

遷移文件(工程100%):

Schema::create('orders', function (Blueprint $table) { 
      $table->integer('order_id')->unsigned()->index(); 
      $table->integer('customer_id')->unsigned()->index(); 
      $table->foreign('customer_id')->references('customer_id')->on('customers')->onDelete('cascade'); 
      $table->string('order_status'); 
      $table->string('customer_note')->nullable(); 
      $table->timestamp('order_date'); 
      $table->timestamps(); 
      $table->softDeletes(); 
      $table->primary(['order_id', 'customer_id']); 
     }); 

在我的模型我已經做了以下幾列可填寫的,這樣Laravel不會忽略他們:

protected $fillable = [ 
     'order_id', 'customer_id', 'order_status', 'customer_note', 'order_date', 
    ]; 

當我創建/更新我在OrderController的update方法下面使用了以下幾行代碼。我使用firstOrCreate()以確保這不會失敗,如果不知何故訂單被刪除或永遠不會添加在首位。

$order = Order::firstOrCreate(['order_id' => $request->input('id')]); 
     $order->order_id = $request->input('id'); 
     $order->customer_id = $request->input('customer_id'); 
     $order->order_status = $request->input('status'); 
     $order->customer_note = $request->input('customer_note'); 
     $order->order_date = $request->input('date_created'); 
     $order->save(); 

當我嘗試更新我在日誌文件中收到以下錯誤消息的順序:

Next Illuminate\Database\QueryException: SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`vcc-backoffice`.`orders`, CONSTRAINT `orders_customer_id_foreign` FOREIGN KEY (`customer_id`) REFERENCES `customers` (`customer_id`) ON DELETE CASCADE) (SQL: insert into `orders` (`order_id`, `updated_at`, `created_at`) values (76, 2017-09-08 08:55:37, 2017-09-08 08:55:37)) in /home/vagrant/Projects/vcc-backoffice/vendor/laravel/framework/src/Illuminate/Database/Connection.php:647 

我注意到,插入語句只試圖插入3列。 order_idupdated_atcreated_at

我假設該行無法創建因爲國外* CUSTOMER_ID ** colun沒有被填滿,但我想不出爲什麼Laravel被忽略它們。

  1. 我想也許,輸入不正確的格式,但即使硬編碼的CUSTOMER_ID爲1沒有工作。 (customer_id 1存在於customers表中)。

  2. 我還檢查並確認$請求 - >輸入(「CUSTOMER_ID」)包含正確的整數,在這種情況下是1,確實存在如客戶表中的記錄。

我懷疑Laravel忽略了相關的列,但我不明白爲什麼。

我的模型文件,如下所示:

namespace App; 

use Illuminate\Database\Eloquent\Model; 
use Illuminate\Notifications\Notifiable; 
use Illuminate\Database\Eloquent\SoftDeletes; 

class Order extends Model 
{ 
    use Notifiable; 
    use SoftDeletes; 

    /** 
    * The attributes that are mass assignable. 
    * 
    * @var array 
    */ 
    protected $fillable = [ 
     'order_id', 'customer_id', 'order_status', 'customer_note', 'order_date', 
    ]; 

    /** 
    * Whether the primary key auto-increments. 
    * 
    * @var bool 
    */ 
    public $incrementing = false; 

    /** 
    * Set the primary key. 
    * 
    * @var string 
    */ 
    protected $primaryKey = ['order_id', 'customer_id']; 

    protected $with = ['products']; 

    /** 
    * The products that belong to the Order. 
    */ 
    public function products() 
    { 
     return $this->belongsToMany('App\Product','order_product','order_id','product_id') 
      ->withPivot('qty') 
      ->withTimeStamps(); 
    } 

    /** 
    * The customer that belongs to the Order. 
    */ 
    public function customer() 
    { 
     return $this->belongsTo('App\Customer'); 
    } 
} 
+0

當您添加外鍵則記錄必須存在於相關表格。請檢查 –

+0

什麼是'protected $ primaryKey = ['order_id','customer_id'];'for? – Hammerbot

+0

@SagarGautam在回答之前請仔細閱讀我的問題。我已經解釋了一切 –

回答

0

firstOrCreate將創建一個新的數據庫條目如果基於你給的屬性不能讀取行,但沒有customer_id創建一個新的行是不允許的因爲你已經在列上添加了一個外鍵,它不能是null

要麼添加null將您的列,

$table->integer('customer_id')->unsigned()->index()->nullable(); 

或更改firstOrCreate這樣:

$order = Order::where('order_id', $request->input('id'))->first() ?: new Order;