Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Michael Fayez's avatar

Property [$post] not found on component: [post.index]

I work in Automatic System where I have to send email to every member when something happened in the Model I used Event and listeners when it comes to delete action it can not work Here is the Deleted Method in Livewire :-

public function delete(Post $post)
    {
        abort_if(Gate::denies('post_delete'), Response::HTTP_FORBIDDEN, '403 Forbidden');

        $post->delete();

        event(new PostDeleted($this->post));
    }

This is PostDeleted Event

<?php

namespace App\Events\Post;

use Illuminate\Broadcasting\Channel;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;
use App\Models\Post;

class PostDeleted
{
    use Dispatchable, InteractsWithSockets, SerializesModels;

    
    public function __construct(public Post $post)
    {
        //
    }

   
    public function broadcastOn(): array
    {
        return [
            new PrivateChannel('channel-name'),
        ];
    }
}

Here is PostDeletedEmail

<?php

namespace App\Listeners\Post;

use App\Notifications\Post\PostDeletedNotification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Support\Facades\Notification;
use App\Models\Project;
use App\Models\Post;

class PostDeletedEmail
{
    
    public function handle(object $event): void
    {
        $post      = $event->post;
        $project   = $post->project;
        $assignees = $project->assignees()->get();

        Notification::send(
            $assignees,
            new PostDeletedNotification($post)
        );
    }
}

Here is the PostDeletedNotification

<?php

namespace App\Notifications\Post;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;
use App\Models\Post;

class PostDeletedNotification extends Notification
{
    use Queueable;

   
    public function __construct(Post $post)
    {
        $this->post = $post;
    }

    
    public function via(object $notifiable): array
    {
        return ['mail'];
    }

    
    public function toMail(object $notifiable): MailMessage
    {
        return (new MailMessage)
            ->subject($notifiable->email, 'Post Deleted')
            ->markdown('emails.posts.deleted', [
                'user'    => $notifiable,
                'post' => $this->post,
            ]);
    }

   
    public function toArray(object $notifiable): array
    {
        return [
            //
        ];
    }
}

How to notify assignees with deleted post without making any errors

0 likes
1 reply
LaryAI's avatar
LaryAI
AI
Best Answer
Level 58

To notify assignees with a deleted post without any errors, you need to make a few changes to your code.

  1. In your delete method in the Livewire component, you are passing the $post parameter to the PostDeleted event constructor. However, in the event, you are using $this->post instead of $post. Update your delete method as follows:
public function delete(Post $post)
{
    abort_if(Gate::denies('post_delete'), Response::HTTP_FORBIDDEN, '403 Forbidden');

    $post->delete();

    event(new PostDeleted($post));
}
  1. In your PostDeletedEmail listener, you are using the object type hint for the $event parameter. Instead, you should type hint it as PostDeleted event. Update your handle method as follows:
public function handle(PostDeleted $event): void
{
    $post      = $event->post;
    $project   = $post->project;
    $assignees = $project->assignees()->get();

    Notification::send(
        $assignees,
        new PostDeletedNotification($post)
    );
}
  1. In your PostDeletedNotification class, you are missing the $post property declaration. Add the following line at the top of the class:
protected $post;

With these changes, the assignees will be notified with a deleted post without any errors.

Please or to participate in this conversation.