[Vuejs]-How to define event with controller method in laravel web.php file?

1👍

Step 1:
please check in your application namespace App\Providers; this path for event to listener mapping.

Step 2:
Add this line in your function:

Event::fire(app()->make('App\Events\YourEventClassName', [$data]));

$data is optional.

Step 3:
Write all the code you want to do in listener, which you have already mapped.

1👍

Alright you can use observers if you want or call event straight from controller.

Here is how you use observer

<?php

namespace App\Observers;


use App\Order;


class OrderObserver
{
/**
 * Listen to the Order created event.
 *
 * @param  Order  $order
 * @return void
 */
public function created(Order $order)
{
    //
}

/**
 * Listen to the Order deleting event.
 *
 * @param  Order  $order
 * @return void
 */
public function deleting(Order $order)
{
    //
}
}

in your AppServiceProvider include Order::observe(OrderObserver::class); in boot method

Now every time you create or delete Order object created or deleting will triggered.

Or if you want to use controller

<?php

namespace App\Http\controller;


class OrderController extends Controller
{

  public function store(Request $request)
  {
     //.......
     //after saving call your
    event(new NewOrderAdded()) //even better pass user object here
  }

}

Leave a comment