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

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度