douba4275 2017-05-02 15:35
浏览 65
已采纳

Laravel 5.4删除时从数据库中删除记录

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.

  • 写回答

1条回答 默认 最新

  • dongyan1491 2017-05-02 18:13
    关注

    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();
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大