微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Laravel 播种机:调用未定义的方法 Illuminate\Notifications\Notification::getConnectionName() 用户通知NotificationUserSeeder:

如何解决Laravel 播种机:调用未定义的方法 Illuminate\Notifications\Notification::getConnectionName() 用户通知NotificationUserSeeder:

我有两个模型之间的多对多关系:

用户

namespace App\Models;


class User extends Model
{
    use HasApiTokens,Notifiable,hasFactory;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    ];

}

通知

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
 
class Notification extends Model
{
    /**
     * @var array
     */
    protected $guarded = ['id'];
    use HasFactory;
    /**
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function user()
    {
        return $this->belongsTo(User::class);
    }

 
}

我为每个模型创建了一个 factory一个调用这两个工厂的 Seeder,以便创建 users 并将通知附加到每个工厂。

NotificationUserSeeder:

<?PHP

namespace Database\Seeders;

use App\Models\Notification;
use App\Models\User;
use Illuminate\Database\Seeder;

class MockNotificationUser extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */


    }
}

我在运行 db:seed 时收到此错误

调用未定义的方法 Illuminate\Notifications\Notification::getConnectionName()

解决方法

由于您已将模型名称命名为 Notification 。并且在种子类中导入时,看起来您已导入

use Illuminate\Notifications\Notification;

代替

use App\Models\Notification;

已更新 由于模型名称和 Illuminate Notification 外观的冲突,问题出在用户模型上。用户模型中未使用通知外观,因此最好删除通知导入。假设您正在使用通知照明,那么您可以将任何一个导入用作别名

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。