如何对多对多数据表 followers 使用模型观察事件?$this->followings ()->sync ($user_ids, false) 无法触发 created?

通过user_user构建了数据表followers,models里也有follower,但是对Follower使用Observer事件无效。

<?php

namespace App\Observers;

use App\Models\Follower;

// creating, created, updating, updated, saving,
// saved,  deleting, deleted, restoring, restored

class FollowerObserver
{
    public function created(Follower $follower)
    {
        $user = $follower->user;
        $user->increment('follower_count', 1);

        $user = $follower->follower_user;
        $user->increment('following_count', 1);
    }

    public function deleted(Follower $follower)
    {
        $user = $follower->user;
        $user->increment('follower_count', -1);

        $user = $follower->follower_user;
        $user->increment('following_count', -1);
    }

}
    public function follow($user_ids)
    {
        if (!is_array($user_ids)) {
            $user_ids = compact('user_ids');
        }
        $this->followings()->sync($user_ids, false);
    }

    public function unfollow($user_ids)
    {
        if (!is_array($user_ids)) {
            $user_ids = compact('user_ids');
        }
        $this->followings()->detach($user_ids);
    }
《L01 基础入门》
我们将带你从零开发一个项目并部署到线上,本课程教授 Web 开发中专业、实用的技能,如 Git 工作流、Laravel Mix 前端工作流等。
《L05 电商实战》
从零开发一个电商项目,功能包括电商后台、商品 & SKU 管理、购物车、订单管理、支付宝支付、微信支付、订单退款流程、优惠券等
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!