如果新表单插入到数据库中,我试图通知用户,但我收到此错误:

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

在我的 Controller 中,我正在这样做:
 $users = App\User::all()->where('role', 3);
 //trigger email notification
 $Prescription = Prescriptions::first();
 $users->notify(new PrescriptionNotification($Prescription));

一直在关注 This 教程,但仍然无济于事。我在用户模型中有 Notifiable。还能做什么?我失去理智是什么导致了这个错误。

根据我的用户类的要求:
<?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');
    }
}

最佳答案

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

你可以这样做

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

关于php - Laravel 方法通知不存在,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/42621900/

10-09 16:44
查看更多