Calling event in Laravel controller

871 views Asked by At

How do you define event controller like in the route? I'm following this tutorial and the code in route is something like this where you can directly access an event.

Route web.php

Route::get('/sender', function(){
    return view('sender');
});
Route::post('/sender', function(){
    $message = request()->message;
    return event(new App\Events\PusherEvent($message));    
});

So I tried to use a controller instead. What I did was :

Route web.php

Route::get('/sender', 'TestingController@index');
Route::post('/sender', 'TestingController@index');

Controller

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class TestingController extends Controller
{
    public function index(Request $message){
        if($message){
            return event(new App\Events\PusherEvent($message));
        }
        return view('sender');
    }
}

But an error appear at controller because of return event(new App\Events\PusherEvent($message));

enter image description here

What is the right way to define the event? Thank you.

0

There are 0 answers