I am trying to make observer, that when I create my property/ad in form I dump some string and info, but instead I just get success message that property/ad is created with no dump and info. Also it inserts that in database. I am just following regular documentation from Laravel. Any help is appreciated. Here is my code.
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 observers only watch for Eloquent events such as Model::create();
So the event is not dispatched because you're using the DB facade to insert values directly and not using the Eloquent ORM
So change your code to use Eloquent instead
$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;
Don't forget to make the properties fillable in your model
or just in your App\Property
protected $guarded = []; // yolo