【问题标题】:Laravel seeder : Call to undefined method Illuminate\Notifications\Notification::getConnectionName()Laravel 播种器:调用未定义的方法 Illuminate\Notifications\Notification::getConnectionName()
【发布时间】:2021-09-01 19:33:48
【问题描述】:

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

用户

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()

【问题讨论】:

    标签: php laravel factory


    【解决方案1】:

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

    use Illuminate\Notifications\Notification;
    

    而不是

    use App\Models\Notification;
    

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

    【讨论】:

    • 如果我删除它的导入然后如何调用我的课程
    • 能否显示 NotificationUserSeeder 的完整代码
    • 是的,请检查现在更新的代码
    • 删除此导入使用 Illuminate\Notifications\Notification;来自用户模型,因为您没有使用
    • 我从用户模型中询问。不是通知播种器
    猜你喜欢
    • 2017-09-28
    • 2023-04-07
    • 2018-08-14
    • 2016-12-31
    • 2018-03-29
    • 2020-07-27
    • 2018-10-03
    • 2015-11-15
    • 2018-06-25
    相关资源
    最近更新 更多