2017-03-06 145 views
2

我想如果一个新的形式插入到数据库来通知用户,但我得到这个错误: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'); 
    } 
} 

回答

2

$users是一个集合,所以你在一个集合,这将导致错误调用notify方法。方法notify只存在于user object instance

你可以做到这一点

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

我有它在那里。 – raqulka

+0

你能分享你的'用户'类吗? –

+0

请看编辑 – raqulka