I'm trying to update an orders table which has a freign key constraint which is linked by customer_id to the customers table.
Migration file (works 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']);
});
In my model I have made the following columns fillable so that Laravel doesn't ignore them:
protected $fillable = [
'order_id', 'customer_id', 'order_status', 'customer_note', 'order_date',
];
When I create/update an order I use the following lines of code in the OrderController under the update method. I use firstOrCreate() to ensure that this doesn't fail if somehow the order was deleted or never added in the first place.
$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();
When I try to update the order I get the following error message in the log file:
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
I noticed that the insert statement only tries to insert 3 columns. order_id
, updated_at
, created_at
I'm assuming that the row cannot be created because the foreign *customer_id** colun isn't being filled but I cannot figure out why Laravel is ignoring them.
I thought perhaps, the input was not in the right format but even hard-coding the customer_id as 1 didn't work. (customer_id 1 exists in customers table).
I've also checked and confirm that $request->input('customer_id') contains the correct integer, which is 1 in this case and does exist as a record in the customers table.
I suspect that Laravel is ignoring the relevant columns but I cannot make out why.
My model file looks as follows:
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');
}
}
firstOrCreate
will create a new database entry if it can't fetch a row based on the attributes you give, but creating a new row without customer_id
is not allowed as you've added a foreign key on the column and it can't be null
.
Either add null
to your column,
$table->integer('customer_id')->unsigned()->index()->nullable();
or change firstOrCreate
to this:
$order = Order::where('order_id', $request->input('id'))->first() ?: new Order;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.