Laravel观察员的数据透视表

时间:2017-07-14 12:54:32

标签: php laravel pivot observers

我有一个有更新方法的观察者:

ObserverServiceProvider.php

public function boot()
{
    Relation::observe(RelationObserver::class);
}

RelationObserver.php

public function updated(Relation $relation)
{
    $this->cache->tags(Relation::class)->flush();
}

所以当我更新控制器中的关系时:

public function update(Request $request, Relation $relation)
{
     $relation->update($request->all()));
     return back();
}

一切都按预期工作。但现在我有了一个数据透视表。 关系 belongsToMany产品。

所以现在我的控制器方法如下所示:

public function update(Request $request, Relation $relation)
{
    if(empty($request->products)) {
        $relation->products()->detach();
    } else {
        $relation->products()->sync(collect($request->products)->pluck('id'));
    }

    $relation->update($request->all());

    return back();
}

问题是,如果我添加或删除产品,则不再触发观察者。

pivot table更新aswel?

时,如何触发观察者?

由于

1 个答案:

答案 0 :(得分:6)

正如您所知,Laravel在调用sync()时并未实际检索模型或调用任何模型上的保存/更新,因此默认情况下不创建任何事件。但我想出了一些替代解决方案来解决你的问题。

1 - 为sync()方法添加一些额外功能:

如果您深入研究belongsToMany功能,您会看到它试图猜测一些变量名称并返回一个BelongsToMany对象。最简单的方法是让你的关系功能只是自己返回一个自定义BelongsToMany对象:

public function products() {

    // Product::class is by default the 1. argument in ->belongsToMany calll
    $instance = $this->newRelatedInstance(Product::class);

    return new BelongsToManySpecial(
        $instance->newQuery(),
        $this,
        $this->joiningTable(Product::class), // By default the 2. argument
        $this->getForeignKey(), // By default the 3. argument
        $instance->getForeignKey(), // By default the 4. argument
        null // By default the 5. argument
    );
}

或者复制整个函数,重命名并使其返回BelongsToManySpecial类。或者省略所有变量,或者简单地返回new BelongsToManyProducts类并解析__construct中的所有BelongsToMany变量......我认为你有了这个想法。

使BelongsToManySpecial类扩展原始的BelongsToMany类,并将同步函数写入BelongsToManySpecial类。

public function sync($ids, $detaching = true) {

    // Call the parent class for default functionality
    $changes = parent::sync($ids, $detaching);

    // $changes = [ 'attached' => [...], 'detached' => [...], 'updated' => [...] ]
    // Add your functionality
    // Here you have access to everything the BelongsToMany function has access and also know what changes the sync function made.

    // Return the original response
    return $changes
}

或者覆盖detachattachNew函数以获得类似的结果。

protected function attachNew(array $records, array $current, $touch = true) {
    $result = parent::attachNew($records, $current, $touch);

    // Your functionality

    return $result;
}

public function detach($ids = null, $touch = true)
    $result = parent::detach($ids, $touch);

    // Your functionality

    return $result;
}

如果你想深入挖掘并希望了解幕后发生的事情,那么分析Illuminate\Database\Eloquent\Concerns\HasRelationship特征 - 特别是belongsToMany关系函数和BelongsToMany类本身。

2 - 创建一个名为BelongsToManySyncEvents的特征,它不会比返回特殊的BelongsToMany类做更多的事情

trait BelongsToManySyncEvents {

    public function belongsToMany($related, $table = null, $foreignKey = null, $relatedKey = null, $relation = null) {

        if (is_null($relation)) {
            $relation = $this->guessBelongsToManyRelation();
        }

        $instance = $this->newRelatedInstance($related);
        $foreignKey = $foreignKey ?: $this->getForeignKey();
        $relatedKey = $relatedKey ?: $instance->getForeignKey();

        if (is_null($table)) {
            $table = $this->joiningTable($related);
        }

        return new BelongsToManyWithSyncEvents(
            $instance->newQuery(), $this, $table, $foreignKey, $relatedKey, $relation
        );
    }

}

创建BelongsToManyWithSyncEvents类:

class BelongsToManyWithSyncEvents extends BelongsToMany {

    public function sync($ids, $detaching = true) {

        $changes = parent::sync($ids, $detaching);

        // Do your own magic. For example using these variables if needed:
        // $this->get() - returns an array of objects given with the sync method
        // $this->parent - Object they got attached to
        // Maybe call some function on the parent if it exists?

        return $changes;
    }

}

现在将特质添加到您的班级。

3 - 结合以前的解决方案,并将此功能添加到您在BaseModel类等中的每个模型中。例如,让它们检查并调用一些方法,以防它被定义...

$functionName = 'on' . $this->foreignKey . 'Sync';

if(method_exists($this->parent), $functionName) {
    $this->parent->$functionName($changes);
}

4 - 创建服务

在该服务中创建一个必须始终调用的函数,而不是默认的sync()。也许称之为attachAndDetachProducts(...)并添加您的事件或功能

由于我没有关于你的课程和关系的大量信息,你可以选择比我提供的更好的课程名称。但是,如果您现在的用例只是清除缓存,那么我认为您可以使用一些提供的解决方案。