Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel Method notify does not exist

Tags:

php

laravel

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 App\Notifications\Admin;

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

use App\Models\Admin\Forms\Prescriptions;

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 \Illuminate\Notifications\Messages\MailMessage
     */
    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 = App\User::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 Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User 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('App\Models\Admin\Forms\Prescription_confirmations', 'physician_id');
    }

    //relationship with prescriptions forms
    public function prescriptions()
    {
        return $this->hasMany('App\Models\Admin\Forms\Prescriptions', 'physician_id');
    }
}
like image 327
z0mbieKale Avatar asked Dec 07 '22 19:12

z0mbieKale


2 Answers

$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));
}
like image 184
Đào Minh Hạt Avatar answered Dec 10 '22 21:12

Đào Minh Hạt


Think it's easier to use Notification Facade.

Notification::send($users, new PrescriptionNotification($Prescription));

And you need to call Notification facade with use like this,

use Illuminate\Support\Facades\Notification;
like image 25
vimuth Avatar answered Dec 10 '22 21:12

vimuth