In this guide, I’ll walk you through how to create a table, insert fake data, and build relationships between tables — all using Laravel’s powerful artisan commands.
Step 1: Install Laravel and Set Up the Database
After installing Laravel, open the .env file and update your database settings:
DB_DATABASE=your_db_name
DB_USERNAME=your_db_user
DB_PASSWORD=your_db_password
Now run:
php artisan migrate
This will create default tables like users, password_resets, etc.
✅ Step 2: Create a New Table (employees)
Let’s say we want to create a new table called employees. Use this command:
php artisan make:migration create_employees_table
Open the new migration file in database/migrations/ and define the table structure like:
Schema::create('employees', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('email');
$table->timestamps(); // important for created_at & updated_at
});
Then run:
php artisan migrate
✅ Step 3: Create Model, Factory, Seeder, and Controller
To save time, generate all at once:
php artisan make:model Employee -c -f -s
Or if you haven’t created a migration file yet:
php artisan make:model Employee -c -f -s -m
This creates:
Model:app/Models/Employee.phpController:app/Http/Controllers/EmployeeController.phpFactory:database/factories/EmployeeFactory.phpSeeder:database/seeders/EmployeeSeeder.phpMigration(optional)
✅ Step 4: Edit Files for Fake Data
Model (Employee.php)
protected $fillable = ['name', 'email'];
Factory (EmployeeFactory.php)
public function definition()
{
return [
'name' => $this->faker->name,
'email' => $this->faker->unique()->safeEmail,
];
}
Seeder (EmployeeSeeder.php)
public function run()
{
\App\Models\Employee::factory()->count(10)->create();
}
Run the seeder:
php artisan db:seed --class=EmployeeSeeder
⚠️ Tip: Make sure
timestamps()is included in your migration; otherwise, factory-generated data won’t work properly.
✅ Step 5: Create Another Table (jobrolls)
Now, repeat the same for jobrolls:
php artisan make:model Jobroll -c -f -m -s
Define the table in the migration:
Schema::create('jobrolls', function (Blueprint $table) {
$table->id();
$table->string('jobname');
$table->timestamps();
});
Update:
Jobrollmodel → fillable propertyJobrollFactory→ definejobnameJobrollSeeder→ use factory to create records
Then run:
php artisan migrate
php artisan db:seed --class=JobrollSeeder
✅ Step 6: Setup Many-to-Many Relationship
Create a pivot table between employees and jobrolls:
php artisan make:migration create_employee_jobroll_table --create=employee_jobroll
Define the pivot table structure:
Schema::create('employee_jobroll', function (Blueprint $table) {
$table->id();
$table->foreignId('employee_id')->constrained()->onDelete('cascade');
$table->foreignId('jobroll_id')->constrained()->onDelete('cascade');
$table->timestamps();
});
Run:
php artisan migrate
Now update the relationship in the models.
In Employee.php
public function jobrolls()
{
return $this->belongsToMany(Jobroll::class);
}
In Jobroll.php
public function employees()
{
return $this->belongsToMany(Employee::class);
}
✅ Step 7: Assign Jobrolls to Employees
You can use Laravel Tinker to assign relationships:
php artisan tinker
Inside tinker:
use App\Models\Employee;
$employee = Employee::find(1);
$employee->jobrolls()->attach([1, 3]); // assign jobrolls with IDs 1 and 3
🔄 Useful Artisan Commands Summary
| Task | Command |
|---|---|
| Create Model + C/F/S/M | php artisan make:model ModelName -c -f -s -m |
| Run migrations | php artisan migrate |
| Reset all tables | php artisan migrate:reset |
| Fresh migrate & seed | php artisan migrate:fresh --seed |
| Run a specific seeder | php artisan db:seed --class=ClassNameSeeder |
| Use Laravel console | php artisan tinker |
💡 Final Thoughts
Laravel makes it super easy to handle database structures, fake data, and relationships — all with clean and readable code. If you’re a beginner, try creating small projects like this to practice working with models and relationships.
Have questions or suggestions? Get in touch with me!
