Markk
2 months ago
371
10
Laravel

Custom Events and listeners, 5.7 from scratch episode 32

Posted 2 months ago by Markk

I kinda hate this moments but at the same time i learn most from them. Anyway i am following Jeffrey at this lecture and i understood everything and did exacltly as he did but when i create a project i only get Event Data through telescope but nor do i have something i Listeners tab, nor i receive an email.

Here is my code:

------------------- ProjectController ---------------------

public function store() {

        $attributes = $this->validateProject();
        
        $attributes['owner_id'] = auth()->id();

        $project = Project::create($attributes);

        event(new ProjectCreated($project));

        return redirect('projects');
    }

---------------------Project Created event---------------------


namespace App\Events;

use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;

class ProjectCreated
{
    use Dispatchable, SerializesModels;

    public $project;

    /**
     * Create a new event instance.
     *
     * @return void
     */
    public function __construct($project)
    {
        $this->project = $project;
    }

}

--------------------- SendProjectCreatedNotification Listener ---------------------



namespace App\Listeners;

use App\Events\ProjectCreated;
use Illuminate\Support\Facades\Mail;
use App\Mail\ProjectCreated as ProjectCreatedMail;

class SendProjectCreatedNotification
{
    /**
     * Create the event listener.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Handle the event.
     *
     * @param  ProjectCreated  $event
     * @return void
     */
    public function handle(ProjectCreated $event)
    {
        
        Mail::to($event->project->owner->email)->send(new ProjectCreatedMail($event->project));

    

    }
}

 --------------------- And registered in EventServiceProvider ---------------------

  protected $listen = [
        Registered::class => [
            SendEmailVerificationNotification::class,
        ],

        ProjectCreated::class => [
            SendProjectCreatedNotification::class
        ]
    ];

Sorry if some of the code is not interpeted as code, i don't know why this is happening or if i'm not doing something right let me know.

Thanks for reading

Please sign in or create an account to participate in this conversation.