Now I need to send message to User whose shop is activated. And also give them a dashboard to add new products.
Goal: When in
shops
table the value of columnis_active
is made toactive
frominactive
then I need to fire the email to user.
Shop.php
public function seller() //user --> seller
{
return $this->belongsTo(User::class, 'user_id');
}
I need observer class so I can find all these information on documentation in event section of Eloquent.
Step 1
Run below command to Create observer class for Shop Model
~$ php artisan make:observer ShopObserver --model=Shop
- Now we have
DealOcean\app\Observers\ShopObserver.php
ShopObserver.php
<?php
namespace App\Observers;
use App\Shop;
class ShopObserver
{
/**
* Handle the shop "created" event.
*
* @param \App\Shop $shop
* @return void
*/
public function created(Shop $shop)
{
//
}
/**
* Handle the shop "updated" event.
*
* @param \App\Shop $shop
* @return void
*/
public function updated(Shop $shop)
{
//
}
/**
* Handle the shop "deleted" event.
*
* @param \App\Shop $shop
* @return void
*/
public function deleted(Shop $shop)
{
//
}
/**
* Handle the shop "restored" event.
*
* @param \App\Shop $shop
* @return void
*/
public function restored(Shop $shop)
{
//
}
/**
* Handle the shop "force deleted" event.
*
* @param \App\Shop $shop
* @return void
*/
public function forceDeleted(Shop $shop)
{
//
}
}
Step 2
Before doing that, we need to tell Laravel that you need to use this class whenever model events related to Shop is updated. To register an observer, use the observe method on the model you wish to observe. You may register observers in the boot method of one of your service providers. In this example, we'll register the observer in the AppServiceProvider:
Go to AppServiceProvider
AppServiceProvider.php
use App\Shop;
use App\Observers\ShopObserver;
public function boot()
{
Shop::observe(ShopObserver::class);
}
Step 3
ShopObserver.php
public function updated(Shop $shop)
{
dd($shop);
}
SO when I update the
is_active
column toactive
frominactive
then this doesn't work.
I mean the it should call the
update
function. But this function is not called.
I can't figure out the problem.
ShopController.php
public function update(Request $request, Shop $shop)
{
Shop::where('id', $request->shop_id)
->update([
'is_active' => $request->is_active,
'description' => $request->description,
'location_id' => $request->location
]);
return Redirect::route('dashboard.shops');
}
is_active
field toactive
. – Tpojka