Skip to content
Readerstacks logo Readerstacks
  • Home
  • Softwares
  • Angular
  • Php
  • Laravel
  • Flutter
Readerstacks logo
Readerstacks
How to run specific seeder class in laravel

How to run specific seeder class in laravel 9 ?

Aman Jain, February 19, 2022February 22, 2022

In our last article we learnt about how to create seeder in laravel but sometimes we only need to run a specific seeders rather then running all so in this article we will cover to run a specific seeder. We will use a simple example of article class and will run only that specific seeder using artisan command.

Below is the command to run specific seeder and we passed an option –class to run specific seeder class

php artisan db:seed --class=NameSeeder

Before start I assume you well known to database and and How to make database connection in Laravel 8 ?

Let’s start the article run specific seeder class with step by step process

Step 1: Create a seeder class

Very first step is to create the seeder class using PHP artisan command as below

php artisan make:seeder ArticleSeeder

Above command will create a file in folder database/seeders and the code will be in file

<?php

namespace Database\Seeders;

use Illuminate\Database\Seeder;

class ArticleSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        //
    }
}

Step 2: Modify Generated Class to Seed the Table

In previous step we created the class, now modify it according to our need for example in our articles table we have below schema

CREATE TABLE `articles` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `email` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
  `title` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
  `picture` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
  `body` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

then we need to change the ArticleSeeder class as below

<?php

namespace Database\Seeders;

use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Str;

class ArticleSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
          for($count=0;$count<10;$count++){
            DB::table('articles')->insert([
                'title' => Str::random(10),
                'email' => Str::random(10).'@gmail.com',
                'body'  => Str::random(100),
                'updated_at' => date("Y-m-d H:i:s"),
                'created_at' => date("Y-m-d H:i:s"),
                
            ]);
        }
    }
}

As you can see we have added the for loop to run the insert query 10 times and it will generate 10 records in article table.

Step 3: Run the seeders

RUn the specific seeder class using artisan command as below

php artisan db:seed --class=ArticleSeeder

This will create 10 records in table.

Related

Php Laravel Laravel 9 databaselaravelseeders

Post navigation

Previous post
Next post

Related Posts

Laravel Laravel Audit Integration

Exploring Laravel Audit Integration: Ensuring Data Security and Accountability

November 9, 2023March 16, 2024

In today’s digital age, data security and accountability are of utmost importance and Laravel Audit most important to keep the records of each changes. Organizations need to keep a close eye on who accesses, modifies, or deletes sensitive information within their applications. Laravel, a popular PHP framework, has a powerful…

Read More
Php file_get_contents Post and Get Request with Headers in Laravel

file_get_contents Post and Get Request with Headers in Laravel ?

June 13, 2022June 13, 2022

In this article i will show you to send file_get_contents post and get request with headers in laravel. As we know file_get_contents is php inbuilt core function used to access the local file and remote URLs. File_get_contents is used to read the file in different modes and to send the…

Read More
Laravel Get Domain Name in Laravel

How to Get Domain Name in Laravel ?

November 27, 2023March 16, 2024

In this blog post we will learn to get domain name in laravel. Laravel inbuilt library for handling the request and response can do this task very seamlessly. A domain name is the human-readable address that users enter into their browsers to access a website. Knowing the domain name is…

Read More

Aman Jain
Aman Jain

With years of hands-on experience in the realm of web and mobile development, they have honed their skills in various technologies, including Laravel, PHP CodeIgniter, mobile app development, web app development, Flutter, React, JavaScript, Angular, Devops and so much more. Their proficiency extends to building robust REST APIs, AWS Code scaling, and optimization, ensuring that your applications run seamlessly on the cloud.

Categories

  • Angular
  • CSS
  • Dart
  • Devops
  • Flutter
  • HTML
  • Javascript
  • jQuery
  • Laravel
  • Laravel 10
  • Laravel 11
  • Laravel 9
  • Mysql
  • Php
  • Softwares
  • Ubuntu
  • Uncategorized

Archives

  • May 2025
  • April 2025
  • October 2024
  • July 2024
  • February 2024
  • January 2024
  • December 2023
  • November 2023
  • October 2023
  • July 2023
  • March 2023
  • November 2022
  • October 2022
  • September 2022
  • August 2022
  • July 2022
  • June 2022
  • May 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021

Recent Posts

  • Understanding High Vulnerabilities: A Deep Dive into Recent Security Concerns
  • Understanding High Vulnerabilities in Software: A Week of Insights
  • Blocking Spam Requests with LaraGuard IP: A Comprehensive Guide
  • Enhancing API Development with Laravel API Kit
  • Exploring the Future of Web Development: Insights from Milana Cap
©2023 Readerstacks | Design and Developed by Readerstacks
Go to mobile version