Ok So we need to create a demo Project and for that we need demo data. We are going to use Laravel8 to solve this problem.
So first thing first create a project if you already have a project then you can skip this Step.
So for creating a project you need composer installed in your system. you can these Videos to get started with composer
To use composer 2 you can run this command after installing composer, In-case you install composer 1.
composer self-update
Ok once you have composer installed run this command where you want your project to be
So for example if you want laravel project to setup in Desktop then
open terminal and navigate to Desktop and run this command
// if you want to create project inside a folder name then pass folder name composer create-project laravel/laravel folder/project Name here //Or if you created a folder and already inside the folder run this to install files in current folder composer create-project laravel/laravel .
You can follow or guide for setting up laravel project. It works with Laravel 8
or check Laravel Official site.
One you have your project up we need to run this command in there using terminal
For this example we are going to generate demo data for category and posts.
So first let's generate files for category
// Here we are asking laravel to create required Model, Seeder and Migration files for category // Notice -mfs means Migration, Factory, Seeder files php artisan make:model Category -mfs // Similarly we run command for generating files for Post php artisan make:model Post -mfs
And files will be created under
So add code like this to generate dummy Post content.
// In migration file under database/migrations add this code and change it as you like public function up() { Schema::create('posts', function (Blueprint $table) { $table->id(); $table->string('title',150); $table->mediumText('content'); $table->foreignId('category_id')->constrained(); $table->timestamps(); }); }
Similarly
In Category migration file
public function up() { Schema::create('categories', function (Blueprint $table) { $table->id(); $table->string('name', 150); $table->timestamps(); }); }
So the fields we are requiring in our database are added in migration files. You can alter if you require something different.
Inside directory database/factory you will see PostFactory and CategoryFactory.
In PostFactory
public function definition() { return [ 'title' => $this->faker->text(25), 'content' => $this->faker->text(240), 'category_id' => mt_rand(1, 10), ]; }
This code will add title of length 25and content of length 240, For categories it will save random numbers 1 to 10.
Similarly For Category
public function definition() { return [ 'name' => $this->faker->text(25) ]; }
Here we add category name of length 25 characters.
Inside database/seeder directory
you will see "Category Seeder" and "Post Seeder" files we add factory files in these files like this
In PostSeeder
public function run() { // will generate 1000 demo posts. Post::factory(1000)->create(); }
And In CategorySeeder
public function run() { // this will add 25 categories. Category::factory(25)->create(); }
Include Seeder files to "Database/Seeders/DatabaseSeeder.php" file. Like this
public function run() { // Seeder files are required to be included here to make them work. $this->call(CategorySeeder::class); $this->call(PostSeeder::class); }
Last but not least add "fillables in Models".
So laravel can insert data
Post Model will look like this
class Post extends Model { use HasFactory; protected $fillable = ['title','content','category_id']; }
Similarly for Category Model
class Category extends Model { use HasFactory; protected $fillable = ['name']; }
All is set make sure you have database selected in ".env" file. and database name matches.
Run command to generate data.
php artisan migrate:fresh --seed // here fresh makes database empty and reinserts data using seeder files.
If all goes well you will see the data