Ali Rasheed
Ali Rasheed

Reputation: 2817

Cannot create notification when row is updated

I am using Laravel Pusher notification and able to add notification when the row is created but having difficulties when row is updated. Here is the code beneath

CategoryObserver

public function updated(Category $category)
{
    $user = Auth::user();
    foreach($user->followers as $follower)
    {
        $follower->notify(new UpdateCategory($user, $category));
    }
}

And UpdateCategory

class UpdateCategory extends Notification
{
use Queueable;

/**
 * Create a new notification instance.
 *
 * @return void
 */
public function __construct()
{
    //
}


public function via($notifiable)
{
    return ['database'];
}
public function toDatabase($notifiable)
{

    return [
        'following_id' => Auth::user()->id,
        'following_name' => Auth::user()->name,
    ];
}
public function toArray($notifiable)
{
    return [
        'id' => $this->id,
        'read_at' => null,
        'data' => [
            'following_id' => Auth::user()->id,
            'following_name' => Auth::user()->name,
        ],
    ];
}
}

Upvotes: 0

Views: 38

Answers (2)

Michael MacDonald
Michael MacDonald

Reputation: 433

Your constructor doesn't use the parameters you're passing into it. Instead, you're requesting them again within the notification.

$follower->notify(new UpdateCategory($user, $category));

It doesn't seem like you need the category in there, but you should be using the $user like this.

///In your controller
$follower->notify(new UpdateCategory($user, $category));

//In UpdateCategory
public function __construct(User $user, Category $category) //Make sure these are "used" at the top
{
    $this->user = $user;
    $this->category = $category;
}

//Which enables you to use the user *within* the notification later
public function toDatabase($notifiable)
{

    return [
        'following_id' => $this->user->id,
        'following_name' => $this->user->name
    ];
}
public function toArray($notifiable)
{
    return [
        'id' => $this->id, //You also aren't really getting this anywhere? Is it supposed to be the category id?
        'read_at' => null,
        'data' => [
            'following_id' => $this->user->id,
            'following_name' => $this->user->name
        ],
    ];
}

I'm not sure if it is completely the issue at hand here, but it is definitely the proper way to use the User inside the notification.

Upvotes: 0

FULL STACK DEV
FULL STACK DEV

Reputation: 15941

public function updated(Category $category)
{
    $user = Auth::user();
    foreach($user->followers as $follower)
    {
        $follower->notify(new UpdateCategory($user, $category));
    }
}

//In your Notification

class UpdateCategory extends Notification
{
use Queueable;

public $user;
public $category;

/**
 * Create a new notification instance.
 *
 * @return void
 */
public function __construct(User $user, Category $category)
{
    //
    $this->user = $user;
    $this->category = $category;
}


public function via($notifiable)
{
    return ['database'];
}
public function toDatabase($notifiable)
{

    return [
        'following_id' => $this->user->id,
        'following_name' => $this->user->name,
    ];
}
public function toArray($notifiable)
{
    return [
        'id' => $this->id,
        'read_at' => null,
        'data' => [
            'following_id' => $this->user->id,
            'following_name' => $this->user->name,
        ],
    ];
}
}

You are using Auth() in a Queueable notifications which is not accessible. Remove Queueable trait from your Notification and Try to Inject User object in Notifications class.

Upvotes: 1

Related Questions