{"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

\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

Then you need to call this class from within DatabaseSeeder.php:<\/p>\n

\r\n\r\nclass DatabaseSeeder extends Seeder {\r\n\r\npublic function run()\r\n{\r\nModel::unguard();\r\n\r\n$this-&gt;call('AdminUserSeeder');\r\n}\r\n\r\n}\r\n\r\n<\/pre>\n

And here’s what my AdminUserSeeder.php class looks like:<\/p>\n

\r\n\r\n&lt;?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' =&gt; 'admin_role_name',\r\n'display_name' =&gt; 'Super Admin',\r\n'description' =&gt; 'The highest level of admin-ness',\r\n]);\r\n\r\n$superadmin = Role::where('name','=','admin_role_name')-&gt;first();\r\n$shrek = User::where('username','=','shrek77')-&gt;first();\r\n\r\nif (!$shrek-&gt;hasRole('admin_role_name')) {\r\n$joe-&gt;attachRole( $superadmin-&gt;id );\r\n}\r\n\r\n$this-&gt;command-&gt;info('Admin user seeded :-)');\r\n}\r\n}\r\n\r\n<\/pre>\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

\r\nphp artisan db:seed --class=AdminUserSeeder\r\n<\/pre>\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":"\nHow to seed a database in Laravel 5 (without the "[ReflectionException] Class ____ does not exist" error) - JoeTannorella.com<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.joetannorella.com\/seed-database-laravel-5-without-reflectionexception-class-____-exist-error\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to seed a database in Laravel 5 (without the "[ReflectionException] Class ____ does not exist" error) - JoeTannorella.com\" \/>\n<meta property=\"og:description\" content=\"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...\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.joetannorella.com\/seed-database-laravel-5-without-reflectionexception-class-____-exist-error\/\" \/>\n<meta property=\"og:site_name\" content=\"JoeTannorella.com\" \/>\n<meta property=\"article:published_time\" content=\"2015-02-24T22:50:24+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-06-28T10:46:57+00:00\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.joetannorella.com\/seed-database-laravel-5-without-reflectionexception-class-____-exist-error\/\",\"url\":\"https:\/\/www.joetannorella.com\/seed-database-laravel-5-without-reflectionexception-class-____-exist-error\/\",\"name\":\"How to seed a database in Laravel 5 (without the \\\"[ReflectionException] Class ____ does not exist\\\" error) - JoeTannorella.com\",\"isPartOf\":{\"@id\":\"https:\/\/www.joetannorella.com\/#website\"},\"datePublished\":\"2015-02-24T22:50:24+00:00\",\"dateModified\":\"2021-06-28T10:46:57+00:00\",\"author\":{\"@id\":\"https:\/\/www.joetannorella.com\/#\/schema\/person\/84eafed4ad6cd6934b006e371c06c7ae\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.joetannorella.com\/seed-database-laravel-5-without-reflectionexception-class-____-exist-error\/\"]}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.joetannorella.com\/#website\",\"url\":\"https:\/\/www.joetannorella.com\/\",\"name\":\"JoeTannorella.com\",\"description\":\"Web Development | Digital Marketing | Business\/Startups | Analytics\/data\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.joetannorella.com\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.joetannorella.com\/#\/schema\/person\/84eafed4ad6cd6934b006e371c06c7ae\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.joetannorella.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/257001162d06da1a465f242dfa80dd7b?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/257001162d06da1a465f242dfa80dd7b?s=96&d=mm&r=g\",\"caption\":\"admin\"},\"url\":\"https:\/\/www.joetannorella.com\/author\/admin\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How to seed a database in Laravel 5 (without the \"[ReflectionException] Class ____ does not exist\" error) - JoeTannorella.com","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.joetannorella.com\/seed-database-laravel-5-without-reflectionexception-class-____-exist-error\/","og_locale":"en_US","og_type":"article","og_title":"How to seed a database in Laravel 5 (without the \"[ReflectionException] Class ____ does not exist\" error) - JoeTannorella.com","og_description":"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...","og_url":"https:\/\/www.joetannorella.com\/seed-database-laravel-5-without-reflectionexception-class-____-exist-error\/","og_site_name":"JoeTannorella.com","article_published_time":"2015-02-24T22:50:24+00:00","article_modified_time":"2021-06-28T10:46:57+00:00","author":"admin","twitter_misc":{"Written by":"admin","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.joetannorella.com\/seed-database-laravel-5-without-reflectionexception-class-____-exist-error\/","url":"https:\/\/www.joetannorella.com\/seed-database-laravel-5-without-reflectionexception-class-____-exist-error\/","name":"How to seed a database in Laravel 5 (without the \"[ReflectionException] Class ____ does not exist\" error) - JoeTannorella.com","isPartOf":{"@id":"https:\/\/www.joetannorella.com\/#website"},"datePublished":"2015-02-24T22:50:24+00:00","dateModified":"2021-06-28T10:46:57+00:00","author":{"@id":"https:\/\/www.joetannorella.com\/#\/schema\/person\/84eafed4ad6cd6934b006e371c06c7ae"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.joetannorella.com\/seed-database-laravel-5-without-reflectionexception-class-____-exist-error\/"]}]},{"@type":"WebSite","@id":"https:\/\/www.joetannorella.com\/#website","url":"https:\/\/www.joetannorella.com\/","name":"JoeTannorella.com","description":"Web Development | Digital Marketing | Business\/Startups | Analytics\/data","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.joetannorella.com\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.joetannorella.com\/#\/schema\/person\/84eafed4ad6cd6934b006e371c06c7ae","name":"admin","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.joetannorella.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/257001162d06da1a465f242dfa80dd7b?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/257001162d06da1a465f242dfa80dd7b?s=96&d=mm&r=g","caption":"admin"},"url":"https:\/\/www.joetannorella.com\/author\/admin\/"}]}},"_links":{"self":[{"href":"https:\/\/www.joetannorella.com\/wp-json\/wp\/v2\/posts\/455"}],"collection":[{"href":"https:\/\/www.joetannorella.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.joetannorella.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.joetannorella.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.joetannorella.com\/wp-json\/wp\/v2\/comments?post=455"}],"version-history":[{"count":7,"href":"https:\/\/www.joetannorella.com\/wp-json\/wp\/v2\/posts\/455\/revisions"}],"predecessor-version":[{"id":546,"href":"https:\/\/www.joetannorella.com\/wp-json\/wp\/v2\/posts\/455\/revisions\/546"}],"wp:attachment":[{"href":"https:\/\/www.joetannorella.com\/wp-json\/wp\/v2\/media?parent=455"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.joetannorella.com\/wp-json\/wp\/v2\/categories?post=455"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.joetannorella.com\/wp-json\/wp\/v2\/tags?post=455"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}