Skip to content
Advertisement

Laravel Method notify does not exist

I am trying to notify user if a new form is inserted to database, but I get this error:

BadMethodCallException in Macroable.php line 74: Method notify does not exist.

This is the notification class

<?php

namespace AppNotificationsAdmin;

use IlluminateBusQueueable;
use IlluminateNotificationsNotification;
use IlluminateContractsQueueShouldQueue;
use IlluminateNotificationsMessagesMailMessage;

use AppModelsAdminFormsPrescriptions;

class PrescriptionNotification extends Notification
{
    use Queueable;

    public $Prescription;

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

    /**
     * Get the notification's delivery channels.
     *
     * @param  mixed  $notifiable
     * @return array
     */
    public function via($notifiable)
    {
        return ['database'];
    }

    /**
     * Get the mail representation of the notification.
     *
     * @param  mixed  $notifiable
     * @return IlluminateNotificationsMessagesMailMessage
     */
    public function toMail($notifiable)
    {
        $url = url('/admin/prescriptions/edit/'.$this->Prescriptions->id);

        return (new MailMessage)
                    ->line('New form')
                    ->action('View', $url)
                    ->line('');
    }

    /**
     * Get the array representation of the notification.
     *
     * @param  mixed  $notifiable
     * @return array
     */
    public function toArray($notifiable)
    {
        return [
            'test' => 'test'
        ];
    }
}

And in my controller I am doing this:

 $users = AppUser::all()->where('role', 3);
 //trigger email notification
 $Prescription = Prescriptions::first();
 $users->notify(new PrescriptionNotification($Prescription));

Been following This tutorial, but still to no avail. I have Notifiable in the user model. What else can be done? I am losing my mind what causes this error.

As requested my User class:

<?php

namespace App;

use IlluminateNotificationsNotifiable;
use IlluminateFoundationAuthUser as Authenticatable;

class User extends Authenticatable
{
    use Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'email', 'password',
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];

    //is admin
    public function isAdmin()
    {
        return $this->role; // this looks for an admin column in your users table
    }

    //relationship with prescription forms
    public function confirmations()
    {
        return $this->hasMany('AppModelsAdminFormsPrescription_confirmations', 'physician_id');
    }

    //relationship with prescriptions forms
    public function prescriptions()
    {
        return $this->hasMany('AppModelsAdminFormsPrescriptions', 'physician_id');
    }
}

Advertisement

Answer

$users is a collection, so you are calling notify method on a collection which will lead to error. And the method notify only exist on user object instance.

You can do this

<?php
foreach ($users as $user) {
    $user->notify(new PrescriptionNotification($Prescription));
}
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement