Laravel分离事件不起作用

时间:2017-09-19 20:53:59

标签: php database laravel many-to-many

我正在删除一个模型如下的过滤器:

namespace App;

use App\Events\FilterDelete;
use Illuminate\Database\Eloquent\Model;

class Filter extends Model
{
    protected $events = [
        'deleting' => FilterDelete::class
    ];

    public function filterGroup()
    {
        return $this->belongsTo('App\FilterGroup');
    }

    public function products()
    {
        return $this->belongsToMany('App\Product', 'product_filters')->withPivot('id', 'quantity', 'price');
    }
}

FilterDelete看起来像这样:

namespace App\Events;

use App\Filter;
use Illuminate\Broadcasting\Channel;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;

class FilterDelete
{
    use Dispatchable, InteractsWithSockets, SerializesModels;

    public $filter;

    /**
     * Create a new event instance.
     *
     * @return void
     */
    public function __construct(Filter $filter)
    {
        $this->filter = $filter;
    }

    /**
     * Get the channels the event should broadcast on.
     *
     * @return \Illuminate\Broadcasting\Channel|array
     */
    public function broadcastOn()
    {
        return new PrivateChannel('channel-name');
    }
}

听众看起来像这样:

namespace App\Listeners;

use App\Events\FilterDelete;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;

class DeleteProductFilter
{
    /**
     * Create the event listener.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Handle the event.
     *
     * @param  FilterDelete  $event
     * @return void
     */
    public function handle(FilterDelete $event)
    {
        $event->filter->products()->detach();
    }
}

当我删除过滤器时,产品不会分离,记录会保留在数据库中。没有错误返回,evetywhing似乎工作正常。谁能告诉我为什么产品没有分离?

0 个答案:

没有答案