Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Laravel seeder call seeder from parent directory

I’m trying to call a seeder from a parent directory within my Laravel 9 project. My seeder locations are:

  • Database/Seeders/Production/Permissions/UserManagement/UserPermissionsTableSeeder
  • Database/Seeders/Production/Permissions/PermissionsGeneratorTableSeeder

It’s my UserPermissionsTableSeeder seeder that I need to call my PermissionsGeneratorTableSeeder from:

<?php

namespace Database\Seeders\Production\Permissions\UserManagement;

use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;

class UserPermissionsTableSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run($companyId = null)
    {
        $roles = [
            'super_admin' => [
                'user_index',
                'user_show',
                'user_store',
                'user_update',
                'user_destroy'
            ],
            'admin' => [
                'user_index',
                'user_show',
                'user_store',
                'user_update'
            ]
        ];

        $this->call(
            
          Database\Seeders\Production\Permissions\PermissionsGeneratorTableSeeder::class,
            false,
            ['roles' => $roles]
        );
    }
}

When I attempt to pass the full path to the call method, I get the following error path:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Target class [Database\Seeders\Production\Permissions\UserManagement\Database\Seeders\Production\Permissions\PermissionsGeneratorTableSeeder] does not exist.

What am I missing?

UPDATE

Here’s the PermissionsGeneratorTableSeeder class:

<?php

namespace Database\Seeders\Production\Permissions;

use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;
use Spatie\Permission\Models\Role;
use Spatie\Permission\Models\Permission;

class PermissionsGeneratorTableSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run($roles = [])
    {
        if (!$roles) {
            return;
        }

        foreach ($roles as $key => $permissions) {
            $role = Role::query();
            $role = $role->where('name', $key);

            if ($key != 'super_admin') {
                $role = $role->where('company_id', $companyId);
            }

            $role = $role->first();

            if (!$role) {
                continue;
            }

            foreach ($permissions as $permission) {
                $discoveredPermission = Permission::where('name', $permission)->first();

                if ($discoveredPermission) {
                    $discoveredPermission->assignRole($role);
                    continue;
                }

                $permissionCreated = Permission::create([
                    'name' => $permission,
                    'guard_name' => config('auth.defaults.guard')
                ]);

                $permissionCreated->assignRole($role);
            }
        }
    }
}

>Solution :

In your call method just add a \ to start of the class full path and you’re good to go

$this->call(            
\Database\Seeders\Production\Permissions\PermissionsGeneratorTableSeeder::class,
            false,
            ['roles' => $roles]
        );
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading