2017-03-06 143 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