Monday, March 6, 2017

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 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.



via raqulka

Advertisement