Laravel 12: Building Custom APIs - Creating API Routes & Migrations
Laravel 12: Tutorials, Guides & Best Practices

Laravel 12: Building Custom APIs - Creating API Routes & Migrations

Laravel 12 REST API - Installing Routes & Creating Migrations

Author
Richard Mendes
March 19, 2025 • 5 mins

Laravel 12 API Tutorial – How to Set Up Routes & Migrations

In this tutorial, we will walk through the process of installing the routes file (api.php) in Laravel 12, as well as creating and migrating migration files. By default, Laravel 12 does not include the api.php file, so we need to manually install it. Below are the steps to install and create migrations.

Setting Up API Routes

Use this command to install the api.php file, which is required for creating APIs in Laravel 12.

php artisan install:api

Creating Migrations

Migrations allow you to define and modify database tables using PHP code. We'll create migrations for countries, categories, and languages.

Create Migrations

Run the following Artisan commands to generate migration files:

php artisan make:migration create_countries_table
php artisan make:migration create_categories_table
php artisan make:migration create_languages_table

Define Table Structure in Migration Files

Open the newly created migration files in database/migrations/ and update them:

create_countries_table.php

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('countries', function (Blueprint $table) {
$table->id();
$table->string('name', 100)->unique();
$table->string('code', 10)->unique();
$table->timestamps();
$table->softDeletes();
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('countries');
}
};

create_categories_table.php

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('categories', function (Blueprint $table) {
$table->id();
$table->string('name', 100)->unique();
$table->timestamps();
$table->softDeletes();
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('categories');
}
};

create_languages_table.php

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('languages', function (Blueprint $table) {
$table->id();
$table->string('name', 100)->unique();
$table->string('code', 10)->unique();
$table->timestamps();
$table->softDeletes();
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('languages');
}
};

Running Migrations

After defining the migrations, run the following command to create the tables in the database:

php artisan migrate

If any migrations fail and you need to recreate them, use the command below.

php artisan migrate:refresh

Latest Articles