{"id":1562,"date":"2024-01-23T05:17:09","date_gmt":"2024-01-23T05:17:09","guid":{"rendered":"https:\/\/www.devopssupport.in\/blog\/?p=1562"},"modified":"2024-02-01T05:24:28","modified_gmt":"2024-02-01T05:24:28","slug":"laravel-a-comprehensive-guide-of-seeders","status":"publish","type":"post","link":"https:\/\/www.devopssupport.in\/blog\/laravel-a-comprehensive-guide-of-seeders\/","title":{"rendered":"Laravel : A Comprehensive Guide of Seeders"},"content":{"rendered":"\n<h3 class=\"wp-block-heading\">What are Laravel Seeders?<\/h3>\n\n\n\n<p>Laravel Seeders are part of Laravel&#8217;s Database Seeder functionality, allowing developers to insert sample or default data into their database tables. This is particularly useful for initializing a fresh database with data necessary for testing or setting up default values in production.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">How Laravel Seeders Work:<\/h4>\n\n\n\n<h3 class=\"wp-block-heading\">Seeder Classes:<\/h3>\n\n\n\n<p>In Laravel, seeders are organized into seeder classes. Each seeder class typically corresponds to a specific database table, and developers can create multiple seeders to populate various tables with different sets of data.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Defining Seed Data:<\/h3>\n\n\n\n<p>Within a seeder class, developers use the <code>run<\/code> method to define the data they want to insert into the associated table. This can include hardcoded values or dynamically generated data using Faker, Laravel&#8217;s built-in fake data generator.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Generate a Seeder Class<\/h3>\n\n\n\n<p>Use the <code>make:seeder<\/code> Artisan command to generate a new seeder class. Replace <code>ExampleTableSeeder<\/code> with the desired name for your seeder and <code>ModelName<\/code> with the name of the Eloquent model associated with the table:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>php artisan make:seeder ExampleTableSeeder\r\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Define Seeder Data<\/h3>\n\n\n\n<p>Open the generated seeder class (located in the <code>database\/seeders<\/code> directory) and define the data you want to insert into the associated table within the <code>run<\/code> method. You can use Laravel&#8217;s query builder or Eloquent methods to interact with the database.<\/p>\n\n\n\n<p>For example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>use Illuminate\\Database\\Seeder;\r\nuse Illuminate\\Support\\Facades\\DB;\r\n\r\nclass ExampleTableSeeder extends Seeder\r\n{\r\n    public function run()\r\n    {\r\n        DB::table('example_table')->insert(&#91;\r\n            'column1' => 'value1',\r\n            'column2' => 'value2',\r\n            \/\/ Additional columns and data\r\n        ]);\r\n    }\r\n}\r\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Run the Seeder<\/h3>\n\n\n\n<p>To execute the seeder and insert the defined data into the database, use the <code>db:seed<\/code> Artisan command. You can specify the seeder class you want to run:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>php artisan db:seed --class=ExampleTableSeeder\r\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Run All Seeders (Optional)<\/h3>\n\n\n\n<p>If you have multiple seeders and want to run all of them at once, you can use the following Artisan command:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>php artisan db:seed\r<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Rollback Seeders (Optional)<\/h3>\n\n\n\n<p>To rollback the last batch of seeders and remove the seeded data from the database, you can use the <code>--reverse<\/code> option:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>php artisan db:seed --class=ExampleTableSeeder --reverse\r<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>What are Laravel Seeders? Laravel Seeders are part of Laravel&#8217;s Database Seeder functionality, allowing developers to insert sample or default data into their database tables. This is&#8230; <\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[1013,1014,1011,1019,1012,1017,42,1016,1015,1018,1020],"class_list":["post-1562","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-databaseinitialization","tag-databasepopulating","tag-databaseseeder","tag-databasetesting-2","tag-dataseeding","tag-eloquentseeder","tag-laravel","tag-laravelartisan","tag-phpframework-2","tag-testdata","tag-webdevelopment-2"],"_links":{"self":[{"href":"https:\/\/www.devopssupport.in\/blog\/wp-json\/wp\/v2\/posts\/1562","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.devopssupport.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.devopssupport.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.devopssupport.in\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.devopssupport.in\/blog\/wp-json\/wp\/v2\/comments?post=1562"}],"version-history":[{"count":1,"href":"https:\/\/www.devopssupport.in\/blog\/wp-json\/wp\/v2\/posts\/1562\/revisions"}],"predecessor-version":[{"id":1563,"href":"https:\/\/www.devopssupport.in\/blog\/wp-json\/wp\/v2\/posts\/1562\/revisions\/1563"}],"wp:attachment":[{"href":"https:\/\/www.devopssupport.in\/blog\/wp-json\/wp\/v2\/media?parent=1562"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopssupport.in\/blog\/wp-json\/wp\/v2\/categories?post=1562"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopssupport.in\/blog\/wp-json\/wp\/v2\/tags?post=1562"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}