jeudi 6 octobre 2016

On delete cascade for pivot table - Eloquent Laravel

I have a many-to-many relation between Appointment and Customer, so 3 tables:

  • appointments
  • customers
  • appointment_customer (pivot)

But when I delete a Customer or an Appointment, it doesn't delete the related rows in my pivot table. Shouldn't any rows related to that deleted Customer (or Appointment), in the appointment_customer table, be deleted also?

My relations are set for delete cascade like this:

appointments migration

Schema::create('appointments', function(Blueprint $table)
{
    $table->increments('id');

appointment model

public function customer()
{
    return $this->belongsToMany('App\Customer')->withPivot('confirmation_sent', 'confirmed')->withTimestamps();
}       

appointments migration

Schema::create('customers', function(Blueprint $table)
{
    $table->increments('id');

customer model

public function appointment()
{
    return $this->belongsToMany('App\Appointment')->withPivot('confirmation_sent', 'confirmed')->withTimestamps();
}       

appointment_customer (pivot) migration

Schema::create('appointment_customer', function(Blueprint $table)
{
    $table->increments('id');

    $table->integer('appointment_id')->unsigned();
    $table->foreign('appointment_id')->references('id')->on('appointments')->onDelete('cascade');

    $table->integer('customer_id')->unsigned();
    $table->foreign('customer_id')->references('id')->on('customers')->onDelete('cascade');

    $table->char('confirmation_sent', 3);        
    $table->char('confirmed', 3); 

    $table->timestamps();



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire