首页 / PHP / php-Laravel方法通知不存在
php-Laravel方法通知不存在
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了php-Laravel方法通知不存在,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2619字,纯文字阅读大概需要4分钟。
内容图文
![php-Laravel方法通知不存在](/upload/InfoBanner/zyjiaocheng/685/895a17f4d48849de9d79039d0e9dcd84.jpg)
我试图通知用户是否将新表单插入数据库,但是出现此错误:
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教程,但仍然无济于事.我的用户模型中有Notifiable.还有什么可以做的?我不知道是什么导致了此错误.
根据要求,我的User类:
<?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仅存在于用户对象实例上.
你可以这样做
<?php
foreach ($users as $user) {
$user->notify(new PrescriptionNotification($Prescription));
}
内容总结
以上是互联网集市为您收集整理的php-Laravel方法通知不存在全部内容,希望文章能够帮你解决php-Laravel方法通知不存在所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。