DEV Community

Cover image for Building Your First API in Laravel – A Beginner’s Guide
Rohit Dhiman
Rohit Dhiman

Posted on

Building Your First API in Laravel – A Beginner’s Guide

APIs (Application Programming Interfaces) are the backbone of modern web and mobile applications. They allow your frontend to communicate with your backend seamlessly. If you’re new to Laravel, building your first API can seem daunting, but don’t worry – this guide will walk you through it step by step.


Why Build APIs in Laravel?

Laravel is one of the most beginner-friendly PHP frameworks, offering:

  • Built-in support for RESTful APIs
  • Simple routing and controller setup
  • Eloquent ORM for database interactions
  • Easy testing with Postman or other API clients

Step 1: Create a New Laravel Project

Use Composer to set up your project:

composer create-project laravel/laravel laravel-first-api
cd laravel-first-api
php artisan serve
Enter fullscreen mode Exit fullscreen mode

Your application will now run at http://127.0.0.1:8000.


Step 2: Set Up the Database

  1. Configure your database in the .env file.
  2. Run migrations to create default tables:
php artisan migrate
Enter fullscreen mode Exit fullscreen mode

Step 3: Create a Model and Migration

php artisan make:model Post -m
Enter fullscreen mode Exit fullscreen mode

In the migration file (database/migrations/..._create_posts_table.php), define your fields:

$table->id();
$table->string('title');
$table->text('content');
$table->timestamps();
Enter fullscreen mode Exit fullscreen mode

Run the migration:

php artisan migrate
Enter fullscreen mode Exit fullscreen mode

Step 4: Create the Controller

php artisan make:controller PostController --api
Enter fullscreen mode Exit fullscreen mode

In app/Http/Controllers/PostController.php, add CRUD methods:

public function index() { return Post::all(); }
public function store(Request $request) { return Post::create($request->all()); }
public function show(Post $post) { return $post; }
public function update(Request $request, Post $post) { $post->update($request->all()); return $post; }
public function destroy(Post $post) { $post->delete(); return response()->noContent(); }
Enter fullscreen mode Exit fullscreen mode

Step 5: Define API Routes

In routes/api.php:

use App\Http\Controllers\PostController;

Route::apiResource('posts', PostController::class);
Enter fullscreen mode Exit fullscreen mode

This automatically creates all routes for CRUD operations.


Step 6: Test Your API

  1. Run the server:
php artisan serve
Enter fullscreen mode Exit fullscreen mode
  1. Open Postman and set http://127.0.0.1:8000 as the base URL.
  2. Test these routes:
  • GET /api/posts – List all posts
  • POST /api/posts – Create a post
  • GET /api/posts/{id} – View a single post
  • PUT /api/posts/{id} – Update a post
  • DELETE /api/posts/{id} – Delete a post

Recap

You’ve successfully:

  • Set up a Laravel project
  • Created models, migrations, and controllers
  • Defined API routes
  • Tested your first API

Next, you can explore authentication with Laravel Sanctum or Passport to secure your API endpoints.


Happy Coding!
If you found this guide helpful, share it and help other beginners get started with Laravel APIs.

#Laravel #PHP #APIDevelopment #WebDevelopment #RESTAPI #BackendDevelopment #BeginnerFriendly #Coding #Programming

Top comments (0)