如何解决无法在 Laravel 的通知文件中保存数据库中的数据
我正在使用 Laravel 通知,如果我编写插入查询,我想将数据保存在我的数据库表中,然后我会收到以下错误,否则通知正在运行:
TypeError:App\User::getLogNameToUse() 的返回值必须是 类型字符串,空
这是查询
public function toMail($notifiable)
{
$users = User::find($notifiable->id);
$users->verification_link=$link;
$users->save();
...
...
}
型号:
<?php
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use App\Notifications\ResetPasswordNotification;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Spatie\Activitylog\Traits\LogsActivity;
class User extends Authenticatable
{
use Notifiable,LogsActivity;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name','email','role_id','password',];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password','remember_token',];
/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
'email_verified_at' => 'datetime',];
public function role()
{
return $this->belongsTo(Models\Role::class);
}
}
解决方法
似乎错误出在 logName
上,如果不查看更多代码和数据,就很难准确找出问题所在。
以下可以工作
/**
* Get the log name to use for the model.
*
* @param string $eventName
* @return string
*/
public function getLogNameToUse(string $eventName = ''): string
{
return Str::kebab(Str::plural(class_basename($this)));
}
这只是覆盖了导致问题的代码段。试试看。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。