>  기사  >  PHP 프레임워크  >  Laravel이 Observer를 사용하여 로그 관리 모듈을 구현하는 방법

Laravel이 Observer를 사용하여 로그 관리 모듈을 구현하는 방법

藏色散人
藏色散人앞으로
2021-07-10 15:27:462167검색

Laravel은 Observer를 사용하여 로그 관리 모듈을 구현합니다.

작성:
로그 관리를 구현하기 위해 여기에 두 개의 기사를 작성했습니다. 첫 번째 기사는 간단한 모델 추가, 삭제 및 수정 모니터링과 로깅. 두 번째 기사에서는 가져온 파일을 통한 일괄 데이터 처리가 어떻게 잘 모니터링되고 처리될 수 없는지 데이터 처리 로직 중 이 부분을 기록하는 방법을 주로 소개합니다. 자세한 내용은 파일을 가져온 후 데이터 변경 사항을 기록하는 Laravel 로그 관리를 참조하세요.

1. 옵저버 파일을 생성합니다. 창고 재고 모듈의 작업 로그를 기록하려고 하므로 다음 명령문을 실행하면 app/Observers 아래에 WarehouseInventoryObserver 파일이 생성됩니다.

php artisan make:observer WarehouseInventoryObserver --model=WarehouseInventory

모델은 app/Models에 위치하므로 경로를 지정해주셔야 합니다.

php artisan make:observer WarehouseInventoryObserver --model=Models/WarehouseInventory

AppProvidersAppServiceProvider

public function boot()
    {
        WarehouseInventory::observe(WarehouseInventoryObserver::class);
    }

2에서 관찰자를 엽니다. 이 모듈에서 추가, 삭제 및 수정 작업을 모니터링합니다. 물론 여기에서 저장소를 사용하거나 모델을 직접 사용할 수 있습니다. 생성, 업데이트 및 삭제는 각각 WarehouseInventory 모델의 신규, 업데이트 및 삭제된 작업을 모니터링합니다.

<?phpnamespace App\Observers;use App\Models\Warehouse;use App\Models\WarehouseInventory;use App\Repositories\ActionLogRepository;use Illuminate\Support\Arr;use Illuminate\Support\Facades\Auth;class WarehouseInventoryObserver{
    protected $user_id;

    protected $warehouse;

    protected $actionLogRepository;

    public function __construct(
        Warehouse $warehouse,
        ActionLogRepository $actionLogRepository
    )
    {
        $this->user_id = Auth::user() ? Auth::user()->id : null;
        $this->warehouse = $warehouse->pluck(&#39;name&#39;, &#39;id&#39;);
        $this->actionLogRepository = $actionLogRepository;
    }

    //创建
    public function created(WarehouseInventory $warehouseInventory)
    {
        if (!empty($this->user_id)) {
            $attributes = $warehouseInventory->getAttributes();
            $attributes = Arr::only($attributes, [&#39;warehouse_id&#39;, &#39;seller_sku&#39;, &#39;quantity&#39;, &#39;box&#39;]);
            $warehouse = $this->warehouse->get($attributes[&#39;warehouse_id&#39;]);
            //拼接数据
            $data = [
                &#39;module&#39; => &#39;warehouse_inventory&#39;,
                &#39;user_id&#39; => $this->user_id,
                &#39;type&#39; => &#39;create&#39;,
                &#39;content&#39; => [
                    &#39;warehouse&#39; => $warehouse,
                    &#39;seller_sku&#39; => $attributes[&#39;seller_sku&#39;],
                    &#39;original_quantity&#39; => 0,
                    &#39;current_quantity&#39; => $attributes[&#39;quantity&#39;],
                    &#39;box&#39; => $attributes[&#39;box&#39;]
                ]
            ];

            $this->actionLogRepository->makeModel()->create($data);
        }
    }

    //更新
    public function updated(WarehouseInventory $warehouseInventory)
    {
        if (!empty($this->user_id)) {
            $original = $warehouseInventory->getOriginal();
            $dirty = $warehouseInventory->getDirty();
            $dirty = Arr::except($dirty, [&#39;remark&#39;, &#39;updated_at&#39;]);
            if (count($dirty)) {
                if (Arr::has($dirty, &#39;warehouse_id&#39;)) {
                    $warehouse = $this->warehouse->get($dirty[&#39;warehouse_id&#39;]);
                } else {
                    $warehouse = $this->warehouse->get($original[&#39;warehouse_id&#39;]);
                }
                //拼接数据
                $data = [
                    &#39;module&#39; => &#39;warehouse_inventory&#39;,
                    &#39;user_id&#39; => $this->user_id,
                    &#39;type&#39; => &#39;update&#39;,
                    &#39;content&#39; => [
                        &#39;warehouse&#39; => $warehouse,
                        &#39;seller_sku&#39; => $original[&#39;seller_sku&#39;],
                        &#39;original_quantity&#39; => $original[&#39;quantity&#39;],
                        &#39;current_quantity&#39; => $dirty[&#39;quantity&#39;],
                        &#39;box&#39; => (Arr::has($dirty, &#39;box&#39;)) ? $dirty[&#39;box&#39;] : $original[&#39;box&#39;]
                    ]
                ];

                $this->actionLogRepository->makeModel()->create($data);
            }
        }
    }

    //删除
    public function deleted(WarehouseInventory $warehouseInventory)
    {
        if (!empty($this->user_id)) {

            $original = $warehouseInventory->getOriginal();
            $warehouse = $this->warehouse->get($original[&#39;warehouse_id&#39;]);
            //拼接数据
            $data = [
                &#39;module&#39; => &#39;warehouse_inventory&#39;,
                &#39;user_id&#39; => $this->user_id,
                &#39;type&#39; => &#39;delete&#39;,
                &#39;content&#39; => [
                    &#39;warehouse&#39; => $warehouse,
                    &#39;seller_sku&#39; => $original[&#39;seller_sku&#39;],
                    &#39;original_quantity&#39; => $original[&#39;quantity&#39;],
                    &#39;current_quantity&#39; => 0,
                    &#39;box&#39; => $original[&#39;box&#39;]
                ]
            ];

            $this->actionLogRepository->makeModel()->create($data);
        }
    }}

3, 데이터베이스
Laravel이 Observer를 사용하여 로그 관리 모듈을 구현하는 방법

관련 추천: 최신 5개의 Laravel 비디오 튜토리얼

위 내용은 Laravel이 Observer를 사용하여 로그 관리 모듈을 구현하는 방법의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
이 기사는 csdn.net에서 복제됩니다. 침해가 있는 경우 admin@php.cn으로 문의하시기 바랍니다. 삭제