Laravel 中加载观察者的问题

我试图让观察者,当我在表单中创建我的财产/广告时,我转储了一些字符串和信息,但我只是收到成功消息,即财产/广告是在没有转储和信息的情况下创建的。它还将其插入到数据库中。我只是遵循 Laravel 的常规文档。任何帮助表示赞赏。这是我的代码。


属性观察器.php


<?php

namespace App\Observers;


use App\Property;


class PropertyObserver

{

    public function created(Property $property)

    {

        dd('Propertyyyyyyyyyy!!!', $property);

    }


    public function updated(Property $property)

    {


    }


    public function deleted(Property $property)

    {


    }

}

应用服务提供者.php


<?php

namespace App\Providers;


use App\Property;

use App\Observers\PropertyObserver;

use Illuminate\Support\Facades\Schema;

use Illuminate\Support\ServiceProvider;


class AppServiceProvider extends ServiceProvider

{

    /**

     * Register any application services.

     *

     * @return void

     */

    public function register()

    {

        $this->publishes([

            base_path().'/config/permissions.php' => config_path('permissions.php'),

        ]);

        $this->publishes([

            base_path().'/config/roles.php' => config_path('roles.php'),

        ]);

    }


    /**

     * Bootstrap any application services.

     *

     * @return void

     */

    public function boot()

    {

        Schema::defaultStringLength(191);

        $this->mergeConfigFrom(

            base_path().'/config/permissions.php', 'permissions'

        );

        $this->mergeConfigFrom(

            base_path().'/config/roles.php', 'roles'

        );


        Property::observe(PropertyObserver::class);

    }

}


慕运维8079593
浏览 217回答 1
1回答

函数式编程

Laravel 观察者只监视 Eloquent 事件,例如Model::create();因此不会调度事件,因为您使用 DB Facade 直接插入值而不使用 Eloquent ORM因此更改您的代码以使用 Eloquent$property_id = $property->create([&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'title' =>&nbsp; $request->title,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'description' => $request->description,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'email' => $request->email,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'number' => $request->number,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'city' => $request->city,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'street' => $request->adresa,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'price' => $request->cena,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'quadrature' => $request->kvadratura,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'main_photo_id' => $main_photo_id,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'main_video_id' => $main_video_id,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'active' => 'Q',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'page_views' => '0',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'user_id' => Auth::user()->id,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'expires_on' =>&nbsp; Carbon::now()->addDays(30),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'created_at' =>&nbsp; Carbon::now(),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'updated_at' =>&nbsp; Carbon::now()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ])->id;不要忘记在您的模型中或仅在您的模型中使属性可填充App\Propertyprotected $guarded = []; // yolo
打开App,查看更多内容
随时随地看视频慕课网APP