Skip to content

5. How to use ? 如何使用?

telan edited this page Nov 21, 2019 · 1 revision

Use the Laravel framework event system to listen for Mysql data changes

  1. To app/Providers/EventServiceProvider.php add event listeners
    /**
     * Event listener mapping for the application.
     *
     * @var array
     */
    protected $listen = [
        
        // Mysql binlog Listener
        \Telanflow\Binlog\Contracts\EventInterface::class => [
            \App\Listeners\BinlogListener::class,
        ],
        
    ];
  1. Create the app/Listeners/BinlogListener.php file
<?php

namespace App\Listeners;

use Telanflow\Binlog\Contracts\EventInterface;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;

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

    /**
     * Handle the event.
     *
     * @param  EventInterface  $event
     * @return void
     */
    public function handle(EventInterface $event)
    {
        // $event->getRecordType(); // 记录ID
        // $event->getRecordTypeName(); // 记录名称:insert、update、delete ...
        // $event->getDatabase(); // 数据库名称
        // $event->getTableName(); // 表名称
        // $event->getPrimaryKey(); // 主键字段名称
        // $event->getData(); // 更新Or插入 的数据
        
        // ... other operate
    }
    
}
Clone this wiki locally