Manually add data into the database it will take time and fussy for the redundant task. So this tedious job reducing by Laravel using seeder.
Let's check the demo of how to add dummy data in category table using Laravel seeder.
First, you need to install faker into Laravel. Faker is a PHP library that generates fake data for you
Install faker using composer command
composer require fzaninotto/faker
After installing faker you can see in require dev parameter in composer.json file in Laravel as below
composer.json file has below configuration:
"require-dev": {
"fzaninotto/faker": "~1.4",
},
Let's check the demo of how to add dummy data in category table using Laravel seeder.
First, you need to install faker into Laravel. Faker is a PHP library that generates fake data for you
Install faker using composer command
composer require fzaninotto/faker
After installing faker you can see in require dev parameter in composer.json file in Laravel as below
composer.json file has below configuration:
"require-dev": {
"fzaninotto/faker": "~1.4",
},
For more information about faker, you can visit the links
Create categories seeder class using below command
php artisan make:seeder CategoriesTableSeeder
Run specific seeder using below command
php artisan db:seed --class=CategoriesTableSeeder
Refresh seeder class using below command with migrate.
php artisan migrate:refresh --seed
Categories Databse: CategoryID, CategoryName, Description - Here CategoryID is primary key
Category Model :
namespace App; use Illuminate\Database\Eloquent\Model; class Category extends Model { public $timestamps=false; }
I am adding 20 records as
1,category1, et sit et mollitia sed.
2,category2, et sit et mollitia sed.
etc into categories table.
1,category1, et sit et mollitia sed.
2,category2, et sit et mollitia sed.
etc into categories table.
use Carbon\Carbon; use Illuminate\Database\Seeder; use Illuminate\Support\Facades\DB; class CategoriesTableSeeder extends Seeder { /** * Run the database seeds. * * @return void */ public function run() { // DB::table('categories')->delete(); $faker = Faker\Factory::create(); for ($i = 1; $i < 20; $i++) { DB::table('categories')->insert([ 'CategoryName' => 'Category'.$i,'Description'=> $faker->text ]); } } }For the run, this CategoriesTableSeeder add this call into DatabaseSeeder class.
use Illuminate\Database\Seeder; class DatabaseSeeder extends Seeder { /** * Run the database seeds. * * @return void */ public function run() { // $this->call(UsersTableSeeder::class); $this->call(CategoriesTableSeeder::class); } }