【问题标题】:Cannot sava data in database in notification file in Laravel无法将数据保存在 Laravel 通知文件中的数据库中
【发布时间】:2021-09-27 18:03:44
【问题描述】:

我正在使用 Laravel 通知,如果我编写插入查询,我想将数据保存在我的数据库表中,然后我会收到以下错误,否则通知正在运行:

TypeError: App\User::getLogNameToUse() 的返回值必须是 输入字符串,null

这是查询

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);
    }
 

}

【问题讨论】:

  • 检查 getLogNameToUse 方法是否存在于您可能正在扩展的模型、特征或父类中
  • @MikeRoss:不,没有方法getLogNameToUse,我正在使用 spatie 包进行日志管理
  • 你可以添加你的用户模型代码吗?
  • @MikeRoss:添加型号代码请检查,Spatie 包链接:spatie.be/docs/laravel-activitylog/v1/advanced-usage/…
  • 给你一个错误 this 部分包代码。你有没有从config/activityLog 文件中修改过任何东西??特别是default_log_name ?

标签: laravel laravel-5 laravel-4


【解决方案1】:

似乎错误在于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)));
}

这只是覆盖导致问题的一段代码。试一试。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-02-24
    • 2017-04-05
    • 1970-01-01
    • 1970-01-01
    • 2019-12-20
    • 1970-01-01
    相关资源
    最近更新 更多