cc分享java:Laravel Observer (ORM 觀察器)
阿新 • • 發佈:2020-06-29
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(){
}
?>