Hi Developers,
Lets see how to create a database seeder in laravel 10. It is more use full in Admin related projects where the register page is not required but still we need admin details for the login the how its done . In this case we can use laravel Seeders for adding data in the database without any register page to add details . Lets see how its done.
All the Seeder files generated in laravel framwork are under the database/seeders folder . By default Seeder class contains only the run() method and when the Seeder class is called the run() method will be executed. We can use Query builders or Eloquent model factories to store data in database through the run() method.
php artisan make:seeder AdminSeeder
Create AdminSeeder using above command and edit the file
database/seeders/AdminUserSeeder.php
<?php
namespace Database\Seeders;
use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;
use App\Models\User;
class AdminSeeder extends Seeder
{
/**
* Run the database seeds.
*/
public function run(): void
{
User::create([
'name' => 'ITSolutionsGuides',
'email' => 'itsolutionsguides@gmail.com',
'password' => bcrypt('123456789'),
]);
}
}
Lets Run the AdminSeeder By using the following command
php artisan db:seed --class=AdminSeeder
Let's Register the AdminSeeder in the DatabaseSeeder
database/seeders/DatabaseSeeder.php
<?php
namespace Database\Seeders;
use Illuminate\Database\Seeder;
class DatabaseSeeder extends Seeder
{
/**
* Seed the application's database.
*/
public function run(): void
{
$this->call(AdminSeeder::class);
}
}
The Following Command will run all the Seeder files registered in DatabaseSeeder
php artisan db:seed
We hope it helps everyone. Thanks for supporting ITSolutionsGuides and keep supporting us also follow us in social media platforms.
Be the first to know about releases and tutorial news and solutions.
We care about your data in our privacy policy.
ITSolutionsGuides was started mainly to provide good and quality web solutions for all the developers. We provide tutorials to support all the developers and also we try to provide solutions to the errors we face while coding.
Copyright © 2023 - 2024 All rights reserved | ITSolutionsGuides