Laravel方法通知不存在

时间:2017-03-06 09:28:19

标签: php laravel

我正在尝试通知用户是否将新表单插入数据库,但是我收到此错误:

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

这是通知类

<?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'
        ];
    }
}

在我的控制器中,我这样做:

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

一直关注This教程,但仍无济于事。我在用户模型中有通知。还有什么可以做的?我正在失去理由导致此错误的原因。

按照我的用户类要求:

<?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');
    }
}

4 个答案:

答案 0 :(得分:3)

$users是一个集合,因此您在集合上调用notify方法会导致错误。方法notify仅存在于user object instance

你可以这样做

<?php
foreach ($users as $user) {
    $user->notify(new PrescriptionNotification($Prescription));
}

答案 1 :(得分:1)

认为使用Notification Facade更容易。

public function store( StoreBooking $request ) {
 $booking = new Booking();
 $booking->bookier_id = $request->bookier_id;
 /**
  add other items
 **/
 $booking->save();
}

您需要使用这种用法来呼叫通知外观

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

答案 2 :(得分:0)

此外,您可以使用higher-order messages

$users->each->notify(new PrescriptionNotification($Prescription));

答案 3 :(得分:0)

就我而言,我忘记实现 Notifiable 特性。

例如,您有一个用户类:

use Illuminate\Notifications\Notifiable;

class Users {
  use Notifiable;

  ...
}