{"id":455,"date":"2015-02-24T22:50:24","date_gmt":"2015-02-24T22:50:24","guid":{"rendered":"https:\/\/www.joetannorella.com\/?p=455"},"modified":"2021-06-28T10:46:57","modified_gmt":"2021-06-28T10:46:57","slug":"seed-database-laravel-5-without-reflectionexception-class-____-exist-error","status":"publish","type":"post","link":"https:\/\/www.joetannorella.com\/seed-database-laravel-5-without-reflectionexception-class-____-exist-error\/","title":{"rendered":"How to seed a database in Laravel 5 (without the “[ReflectionException] Class ____ does not exist” error)"},"content":{"rendered":"
Database seeding in Laravel is pretty awesome. It had me pondering recently what the best practice is for seeding a database with an admin user (or users). During my development I run into a slight issue. I felt like the Laravel docs didn’t fully explain the process, so I’m going to show you my solution below.<\/p>\n
Originally I was adding my seed class to the DatabaseSeeder.php file simply as an extra class. However, this is wrong. New seed classes need to go into their own file. I repeat; your own new seed classes need to go into their own file.<\/strong><\/p>\n Let’s say I’m using the amazing Entrust<\/a> package and want to seed my database with one admin user.\u00a0First you need to create a new seed class file in the same directory as DatabaseSeeder.php. For example:<\/p>\n Then you need to call this class from within DatabaseSeeder.php:<\/p>\n And here’s what my AdminUserSeeder.php class looks like:<\/p>\n Dont’ forget that in Laravel 5 you’ll need to add all the ‘use’ statement at the start of the file to prevent the “Class not found” errors.<\/p>\n As an added bonus, make sure you make full use of “Command->info” to echo into the terminal some info about the success\u00a0of the seed.<\/p>\n Now, when I run:<\/p>\n My database gets seeded, and I get a nice little message back telling me what’s happened.\u00a0I also\u00a0start\u00a0searching more details about different\u00a0databases including\u00a0NoSQL database<\/a>.<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":" Database seeding in Laravel is pretty awesome. It had me pondering recently what the best practice is for seeding a database with an admin user (or users). During my development I run into a slight issue. I felt like the Laravel docs didn’t fully explain the process, so I’m going to show you my solution…<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[7],"tags":[12],"yoast_head":"\n\r\n\r\n\/App\r\n\r\n\/Database\r\n\r\n\/Database\/Seeds\r\n\r\n\/Database\/Seeds\/DatabaseSeeder.php\r\n\r\n\/Database\/Seeds\/AdminUserSeeder.php\r\n\r\n<\/pre>\n
\r\n\r\nclass DatabaseSeeder extends Seeder {\r\n\r\npublic function run()\r\n{\r\nModel::unguard();\r\n\r\n$this->call('AdminUserSeeder');\r\n}\r\n\r\n}\r\n\r\n<\/pre>\n
\r\n\r\n<?php\r\n\r\nuse Illuminate\\Database\\Seeder;\r\nuse Illuminate\\Database\\Eloquent\\Model;\r\n\r\nuse Zizaco\\Entrust\\EntrustRole;\r\nuse Zizaco\\Entrust\\EntrustPermission;\r\nuse Zizaco\\Entrust\\HasRole;\r\nuse App\\Models\\User;\r\n\r\nclass AdminUserSeeder extends Seeder {\r\npublic function run()\r\n{\r\nRole::firstOrCreate([\r\n'name' => 'admin_role_name',\r\n'display_name' => 'Super Admin',\r\n'description' => 'The highest level of admin-ness',\r\n]);\r\n\r\n$superadmin = Role::where('name','=','admin_role_name')->first();\r\n$shrek = User::where('username','=','shrek77')->first();\r\n\r\nif (!$shrek->hasRole('admin_role_name')) {\r\n$joe->attachRole( $superadmin->id );\r\n}\r\n\r\n$this->command->info('Admin user seeded :-)');\r\n}\r\n}\r\n\r\n<\/pre>\n
\r\nphp artisan db:seed --class=AdminUserSeeder\r\n<\/pre>\n