Laravel seeder : Call to undefined method Illuminate\Notifications\Notification::getConnectionName()

Viewed 434

I have a many to many relationship between two models:

User

namespace App\Models;


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

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

}

Notification

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

 
}

I created a factory for each model and a Seeder which calls the two factories in order to create users and attach notifications to each one of them.

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
     */


    }
}

I am getting this error when running db:seed:

Call to undefined method Illuminate\Notifications\Notification::getConnectionName()

1 Answers

Since you have named model name as Notification .And while importing in seeder class ,it look like you have imported

use Illuminate\Notifications\Notification;

instead of

use App\Models\Notification;

Updated The issue is with User model due to conflict in model name and Illuminate Notification facade's. Notification facade not used in User model so better to remove notification import. Suppose if you are using Notification illuminate then you can use as alias any one of the imports

Related