Laravel provides a convenient way of migration. We can create migration and model with one command. But did you know that we can create a model, migration, controller, and factory with one command? Now in this tutorial, you will see create model controller and migration laravel in one command.
So let's see laravel 9 create model with migration and controller by one command. So let's run the below command:
php artisan make:model Country -mr
Here -m
model migrations and r
for resource controller for this Country model.
PS D:\xampp\htdocs\example-app> php artisan make:model Country -mr
INFO Model [D:\xampp\htdocs\example-app\app/Models/Country.php] created successfully.
INFO Migration [D:\xampp\htdocs\example-app\database\migrations/2022_12_07_062924_create_countries_table.php] created successfully.
INFO Controller [D:\xampp\htdocs\example-app\app/Http/Controllers/CountryController.php] created successfully.
App\Models\Country.php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Country extends Model
{
use HasFactory;
}
App\Http\Controllers\CountryController.php
<?php
namespace App\Http\Controllers;
use App\Models\Country;
use Illuminate\Http\Request;
class CountryController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
//
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
//
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
//
}
/**
* Display the specified resource.
*
* @param \App\Models\Country $country
* @return \Illuminate\Http\Response
*/
public function show(Country $country)
{
//
}
/**
* Show the form for editing the specified resource.
*
* @param \App\Models\Country $country
* @return \Illuminate\Http\Response
*/
public function edit(Country $country)
{
//
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param \App\Models\Country $country
* @return \Illuminate\Http\Response
*/
public function update(Request $request, Country $country)
{
//
}
/**
* Remove the specified resource from storage.
*
* @param \App\Models\Country $country
* @return \Illuminate\Http\Response
*/
public function destroy(Country $country)
{
//
}
}
database/migrations/countries_migration
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('countries', function (Blueprint $table) {
$table->id();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('countries');
}
};
Now one more thing, we can create a country factory also with one command. Just run this below command:
php artisan make:model Country -mrf
This command will generate country factory also like:
PS D:\xampp\htdocs\example-app> php artisan make:model Country -mrf
INFO Model [D:\xampp\htdocs\example-app\app/Models/Country.php] created successfully.
INFO Factory [D:\xampp\htdocs\example-app\database/factories/CountryFactory.php] created successfully.
INFO Migration [D:\xampp\htdocs\example-app\database\migrations/2022_12_07_062924_create_countries_table.php] created successfully.
INFO Controller [D:\xampp\htdocs\example-app\app/Http/Controllers/CountryController.php] created successfully.
Database\Factories\CountryFactory.php
<?php
namespace Database\Factories;
use Illuminate\Database\Eloquent\Factories\Factory;
/**
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Country>
*/
class CountryFactory extends Factory
{
/**
* Define the model's default state.
*
* @return array<string, mixed>
*/
public function definition()
{
return [
//
];
}
}
Conclusion
I have tried to discuss the clear concept of laravel 9 create model with migration and controller. Now we know create model controller and migration laravel in one command. Hope this laravel create controller model migration resource tutorial will help you.