Home > front end >  Laravel seeder call seeder from parent directory
Laravel seeder call seeder from parent directory

Time:01-13

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:

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

CodePudding user response:

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]
        );
  • Related