Observer
//注册观察器
App\Providers\AppServiceProvider.php;
class AppServiceProvider extends ServiceProvider
{
public function boot(){
$this->addModelLog();
}
public function addModelLog(){
User::observe(Observer::class);
}
}
App\Observers\Observer.php
<?php
namespace App\Observers;
use Illuminate\Database\Eloquent\Model;
use App\Jobs\OrmObserver;
class Observer{
插入前
public function creating(Model $model){
}
//插入后
public function created(Model $model){
}
//修改前
public function updating(Model $model){
}
//修改后
public function updated(Model $model){
}
//删除前
public function deleting(Model $model){
}
//删除后
public funtion deleted(Model $model){
}
public funtion ObserverJob(){
OrmObserver::dispatch(json_encode($data))->onConnection('redis')->onQueue('ObserverQueue');
}
}
?>
App\Job\OrmObserver;
<?php
namespace App\Jobs;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Log;
use Modules\Crm\Models\CrmOrmSqlLog;
class OrmObserver implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
protected $data = [];
public function __construct($data){
$this->data = json_decode($data)
}
public function handle(){
}
?>