0

I have a reminder table that has a one to many relation on itself, so i have this table:
id: primary key
reminder_id: my foreign key to reminder table
so that i could have a record with key id=1, and many other records that refers to it with reminder_id=1.

I also have a user table, with his id, and a reminder_user to hanlde many to many relationship.
The relation are working good, so that from the user i can retrieve his reminders, from a reminder his children reminder and so on.

The Reminder model:

class Reminder extends Model {

    public function users() {
        return $this->belongsToMany("App\Models\User");
    }

    public function reminder() {
        return $this->belongsTo("App\Models\Reminder");
    }

    public function reminders() {
        return $this->hasMany("App\Models\Reminder");
    }

}

The user model:

class User extends Authenticatable
{
    use Notifiable;

    public function reminders() {
        return $this->belongsToMany("App\Models\Reminder");
    }
}

My problem is that when i update a reminder, the record in my pivot table reminder_user are not deleted.

i tried to create this observer:

namespace App\Observers;

use App\Models\Reminder;

class ReminderObserver {
    public function deleting(Reminder $reminder)
    {
        $reminder->users()->detach();
    }
}

and added it to the appservice provider:

 public function boot()
{
    //...
    Reminder::observe(ReminderObserver::class);
}

but, so far, the records in reminder_user table are not being deleted when i do this transaction:

    DB::transaction(function()use($reminder, $repeated, $user) {
        //delete old reminders
        $reminder->reminders()->delete();
        //add many new reminders
        $reminder->reminders()->saveMany($repeated);
        //save new relation with user
        $user->reminders()->saveMany($repeated);
        //update current reminder
        $reminder->save();
    });

update

As from the answer, the problem is that calling delete() on the query builder doesn't trigger the model observers.

GiuServ
  • 1,215
  • 1
  • 13
  • 33
  • Possible duplicate of [Laravel Eloquent ORM - Many to Many Delete Pivot Table Values left over](http://stackoverflow.com/questions/27330551/laravel-eloquent-orm-many-to-many-delete-pivot-table-values-left-over) – Spholt May 02 '17 at 16:08

1 Answers1

3

Model observers are only called when the action is taken on the model.

Your delete call:

//delete old reminders
$reminder->reminders()->delete();

This is calling delete() on the query builder, so the models are not created, and therefore the model observers will not be triggered. You will need to load each model and call delete on it individually in order for your model observer to trigger:

//delete old reminders
$reminder->reminders()->get()->each(function ($child) {
    $child->delete();
});
patricus
  • 59,488
  • 15
  • 143
  • 145
  • That's working, thanks. Just a note: firstly the `$event` definition in reminder model was missing: `protected $events = ['deleting' => ReminderObserver::class];` Second: i needed a `composer dump-autoload` for the observer to be found. – GiuServ May 03 '17 at 06:27