在 Laravel 中加载观察者时出现问题

Problem with loading observers in Laravel

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

PropertyObserver.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)
    {

    }
}

AppServiceProvider.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);
    }
}

PropertyController.php

public function store(StorePropertyInfo $request, Property $property, Video $video)
{
    $request->validated();

    $categories = DB::table('categories')
    ->whereIn(
        'category',
        [
            $request->propertyType,
            $request->propertyBidAsk,
            $request->propertyPayment
        ]
    )
    ->pluck('id')
    ->toArray();

    $main_video_id =  $video->where('filename_video', $request->main_video)
                            ->get()->pluck('id')->first();
    $main_photo_id = Photo::where('filename', $request->main_photo)
                            ->get()->pluck('id')->first();
    $property_id = $property->insertGetId([
                        'title' =>  $request->title,
                        'description' => $request->description,
                        'email' => $request->email,
                        'number' => $request->number,
                        'city' => $request->city,
                        'street' => $request->adresa,
                        'price' => $request->cena,
                        'quadrature' => $request->kvadratura,
                        'main_photo_id' => $main_photo_id,
                        'main_video_id' => $main_video_id,
                        'active' => 'Q',
                        'page_views' => '0',
                        'user_id' => Auth::user()->id,
                        'expires_on' =>  Carbon::now()->addDays(30),
                        'created_at' =>  Carbon::now(),
                        'updated_at' =>  Carbon::now()
                    ]);

    $property->find($property_id)->category()->attach($categories);

    $image_arr = array_filter(explode(',', $request->image), 'strlen');
    $photoExtensions = ['jpg', 'jpeg', 'png'];
    $videoExtensions = ['mp4', '3gp', 'wmv', 'flv', 'avi'];

    foreach ($image_arr as $key => $value) {
        $file = explode('.', $value);
        $ext = end($file);

        if (in_array($ext, $photoExtensions)) {

            DB::table('photos')->where('filename', $value)
                ->update([
                        'model_type' => 'App\Property',
                        'model_id' => $property_id,
                        'original_src' => "property/$property_id/gallery",
                        'primary' => '0',
                        'updated_at' =>  Carbon::now()
                    ]);

            Storage::move(
                "public/tmp/$value",
                "public/property/$property_id/gallery/$value"
            );
        }

        if (in_array($ext, $videoExtensions)) {
            DB::table('videos')->where('filename_video', $value)
                ->update([
                        'model_type' => 'App\Property',
                        'model_id' => $property_id,
                        'original_src' => "property/$property_id/gallery",
                        'primary' => '0',
                        'updated_at' =>  Carbon::now()
                    ]);

            Storage::move(
                "public/tmp/$value",
                "public/property/$property_id/gallery/$value"
            );
        }
    }

    return redirect()
    ->route('property.index')
    ->with('message', 'Property created successfully');
}

Laravel 观察者只观察 Eloquent 事件,例如 Model::create();
所以事件没有被调度,因为你使用 DB facade 直接插入值而不是使用 Eloquent ORM
因此,将您的代码更改为使用 Eloquent 而不是

$property_id = $property->create([
                        'title' =>  $request->title,
                        'description' => $request->description,
                        'email' => $request->email,
                        'number' => $request->number,
                        'city' => $request->city,
                        'street' => $request->adresa,
                        'price' => $request->cena,
                        'quadrature' => $request->kvadratura,
                        'main_photo_id' => $main_photo_id,
                        'main_video_id' => $main_video_id,
                        'active' => 'Q',
                        'page_views' => '0',
                        'user_id' => Auth::user()->id,
                        'expires_on' =>  Carbon::now()->addDays(30),
                        'created_at' =>  Carbon::now(),
                        'updated_at' =>  Carbon::now()
                    ])->id;

不要忘记在您的模型中使属性可填充
或者就在你的 App\Property

protected $guarded = []; // yolo