Building a REST API with Laravel: A Complete Guide
API routes, resources, Form Request validation, correct status codes, and Sanctum auth — a real, production-reasonable Laravel API.
If you're coming from plain PHP or another framework, Laravel's first few minutes can feel like a lot — artisan commands, service providers, a routes folder, an .env file. This guide walks through exactly what you need to get a real project running, and what each piece actually does.
You need PHP 8.2+, Composer, and a database (MySQL, PostgreSQL, or SQLite for local work). Create a new project with:
composer create-project laravel/laravel my-app
cd my-app
php artisan serve
That last command boots a local dev server at http://localhost:8000. Behind the scenes, Composer just downloaded the framework and a starter folder structure — nothing magic yet.
app/Http/Controllers — where request handling logic livesapp/Models — your Eloquent models (database tables as PHP classes)routes/web.php — every URL your app responds toresources/views — Blade templates (HTML with PHP sprinkled in)database/migrations — version-controlled database schema changesEverything else (config, storage, bootstrap) you'll rarely touch as a beginner.
Routes map a URL to a piece of code. The simplest version lives directly in routes/web.php:
Route::get('/hello', function () {
return 'Hello, Laravel!';
});
For anything beyond a one-liner, move the logic into a controller:
php artisan make:controller GreetingController
class GreetingController extends Controller
{
public function index()
{
return view('greeting', ['name' => 'World']);
}
}
Then point the route at it: Route::get('/hello', [GreetingController::class, 'index']);
Instead of clicking through phpMyAdmin, Laravel lets you define tables in PHP and version-control them:
php artisan make:migration create_posts_table
Schema::create('posts', function (Blueprint $table) {
$table->id();
$table->string('title');
$table->text('body');
$table->timestamps();
});
Run php artisan migrate and the table exists — and anyone who clones your repo can recreate the exact same schema with one command.
Once this clicks, the natural next steps are Eloquent relationships, form validation with Form Requests, and Blade components. Don't try to learn the whole framework at once — build one small real feature (a contact form, a simple blog) and let the rest come up naturally as you need it.
API routes, resources, Form Request validation, correct status codes, and Sanctum auth — a real, production-reasonable Laravel API.
One-to-many, many-to-many, polymorphic, and the N+1 query trap that catches almost every Laravel developer at least once.
Why anything that talks to the outside world belongs in a queued job, and how to dispatch, delay, and chain them correctly.