Please provide your code.
not working laravel seed
working with laravel 7 and using php artisan db:seed command to table seed. then in terminal displayed database seed succesfully message. but not data table filled with data. how could I fix this problem?
Assuming you have created a Seeder class, you need to either call that class directly
php artisan db:seed --class=UsersTableSeeder
or otherwise, add it to the called Seeders in the DatabaseSeeder class, and everything there will be executed using php artisan db:seed:
// database/seeds/DatabaseSeeder.php
public function run()
{
$this->call(UsersTableSeeder::class);
}
@tykus I did not used seeder class
What did you use in that case; the existing DatabaseSeeder?
@tykus my existing database seeder is
<?php
use Illuminate\Database\Seeder;
class DatabaseSeeder extends Seeder
{
/**
* Seed the application's database.
*
* @return void
*/
public function run()
{
// $this->call(UsersTableSeeder::class);
}
}
Hi @flex
You need to uncomment this line (remove the two slashes at the start):
$this->call(UsersTableSeeder::class);
If you customized the default users table you might want to edit UsersTableSeeder to account for those changes.
I did not used seeder class
Where have you actually implemented the seeding?
@tykus now I am using following seeding file
<?php
use Illuminate\Database\Seeder;
class DatabaseSeeder extends Seeder
{
/**
* Seed the application's database.
*
* @return void
*/
public function run()
{
// $this->call(DatabaseTableSeeder::class);
$this->call(LaratrustSeeder::class);
}
}
now My seeding is working with filling only some tables. but the following tables are not filling with data.
users,role_user,permission_user, what is the problem?
Write seeders for those tables:
php artisan make:seeder UsersTableSeeder
php artisan make:seeder RoleUserTableSeeder
php artisan make:seeder PermissionUserTableSeeder
and remember to add them to the DatabaseSeeder run method:
public function run()
{
$this->call(LaratrustSeeder::class);
$this->call(UsersTableSeeder::class);
$this->call(RoleUserTableSeeder::class);
$this->call(PermissionUserTableSeeder::class);
}
@tykus it is not success for me same problem here. please check my LaratrustSeeder.php and wether is it correct or not?
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;
use Illuminate\Support\Facades\Config;
class LaratrustSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
$this->command->info('Truncating User, Role and Permission tables');
$this->truncateLaratrustTables();
$config = config('laratrust_seeder.roles_structure');
$mapPermission = collect(config('laratrust_seeder.permissions_map'));
foreach ($config as $key => $modules) {
// Create a new role
$role = \App\Role::firstOrCreate([
'name' => $key,
'display_name' => ucwords(str_replace('_', ' ', $key)),
'description' => ucwords(str_replace('_', ' ', $key))
]);
$permissions = [];
$this->command->info('Creating Role '. strtoupper($key));
// Reading role permission modules
foreach ($modules as $module => $value) {
foreach (explode(',', $value) as $p => $perm) {
$permissionValue = $mapPermission->get($perm);
$permissions[] = \App\Permission::firstOrCreate([
'name' => $permissionValue . '-' . $module,
'display_name' => ucfirst($permissionValue) . ' ' . ucfirst($module),
'description' => ucfirst($permissionValue) . ' ' . ucfirst($module),
])->id;
$this->command->info('Creating Permission to '.$permissionValue.' for '. $module);
}
}
// Attach all permissions to the role
$role->permissions()->sync($permissions);
if(Config::get('laratrust_seeder.create_users')) {
$this->command->info("Creating '{$key}' user");
// Create default user for each role
$user = \App\User::create([
'name' => ucwords(str_replace('_', ' ', $key)),
'email' => $key.'@app.com',
'password' => bcrypt('password')
]);
$user->attachRole($role);
}
}
}
/**
* Truncates all the laratrust tables and the users table
*
* @return void
*/
public function truncateLaratrustTables()
{
Schema::disableForeignKeyConstraints();
DB::table('permission_role')->truncate();
DB::table('permission_user')->truncate();
DB::table('role_user')->truncate();
if(Config::get('laratrust_seeder.truncate_tables')) {
\App\Role::truncate();
\App\Permission::truncate();
}
if(Config::get('laratrust_seeder.truncate_tables') && Config::get('laratrust_seeder.create_users')) {
\App\User::truncate();
}
Schema::enableForeignKeyConstraints();
}
}
@tykus I have monitored that In my terminal it is displaying this one Truncating User, Role and Permission tables
I might come late to the party. But I am using Laratrust too in my Laravel 8 application.
I use UserSeeder.php to generate my dummy account. Then Assign it to the desired role. To run it, simply run php artisan db:seed --class=UserSeeder and your data is ready.
Hopes this help you @flex
Please or to participate in this conversation.