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

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

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化