Insert Data In Database Using Seeder and Model In Laravel 9

There are many different perspectives to insert data in the Database using Seeder and Model in Laravel 8, 9. The following code example discusses the various solutions. My main focus will be inserting data in the Database using Seeder and Model.

For inserting data in the Database we will need  mainly the three below files:

  1. Model
  2. migration file
  3. seeder file

Be sure you have set up your laravel projects and configured .env file, if you want to know how to set up laravel projects from the basic level you have to read my other article.

Now we will create a model and we will pass -mfs (flag)

-mfs (flag): it will create migration, factory, and seeder files too, in this article we will not use factory so you can pass only -ms (flag);


Now open your command line and run the below command

php artisan make:model Post -mfs

it will create four file model, migration, factory, and seeder

OR

php artisan make:model Post -ms

it will create three file model, migration and seeder

OR

Alternatively you can also create the above file separately one by one 

php artisan make:model Post

it will create a Post.php model

php artisan make:migration create_posts_table

it will create a migration file for the posts table 

php artisan make:seeder PostSeeder

it will create a PostSeeder file for the posts table 

php artisan make:factory PostFactory

it will create a PostFactory file for the posts table 

Insert Data In Database Using Seeder and Model In Laravel 9

Now open your model migration file and add below line of code.

        
        public function up()
    {
        Schema::create('posts', function (Blueprint $table) {
            $table->id();
            $table->string('title');
            $table->text('description');
            $table->timestamps();
        });
    }
        
    

Now open your model Post.php and add fillable property or just assign $guarded variable a blank array it will allow you to insert data using model

        
        <?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
    use HasFactory;

    // protected $fillable = [
    //     'title',
    //     'description'
    // ];

    //Use above or below line

    protected $guarded = [];

}
        
    

Related Post

Now open your model PostSeeder.php file and insert the below line of code.

        
        <?php
namespace Database\Seeders;
use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;
use App\Models\Post;

class PostSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        Post::create([
          'title' => "Test title 01",
          'description' => "Lorem ipsum dolor sit amet",
        ]);
    }
}
        
    

Now open your model DatabaseSeeder.php file and call your Postseeder class 


        
        <?php
namespace Database\Seeders;
// use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;

class DatabaseSeeder extends Seeder
{
    /**
     * Seed the application's database.
     *
     * @return void
     */
    public function run()
    {

        $this->call([
            PostSeeder::class,
            //other seeder class
        ]);
        // \App\Models\User::factory(10)->create();

        // \App\Models\User::factory()->create([
        //     'name' => 'Test User',
        //     'email' => '[email protected]',
        // ]);
    }
}
        
    

That's it, finally we have to run migrate command for generating all tables, and run the below command for running the migration.

php artisan migrate

And now run the seeder command for inserting data that you have defined in the seeder file

php artisan db:seed

You can also run the below command for seeding specific class

php artisan db:seed --database=mysql --seed=PostsTableSeeder

Now you can see your database if everything will be fine you can see the below database structure


Leave a comment