Laravel makes it easy to build RESTful APIs using its powerful routing and Eloquent ORM.
You can create APIs by defining routes in api.php
, creating controllers, and using models for database interaction.
Laravel’s apiResource
automatically sets up CRUD routes.
Data can be returned as JSON using Laravel’s response helpers.
Authentication can be added with Sanctum or Passport for secure API access.
Step-by-Step: Create REST API in Laravel
Step 1: Create a New Laravel Project (if not already)
Put this comment in your project terminal:
composer create-project laravel/laravel laravel-api
cd laravel-api
Step 2: Create a Migration and Model
Let’s say we’re building an API for Products.
Put this comment in your project terminal:
php artisan make:model Product -m
Then edit the migration file:
database/migrations/xxxx_xx_xx_create_products_table.php
// database/migrations/xxxx_xx_xx_create_products_table.php
public function up()
{
Schema::create('products', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->text('description')->nullable();
$table->decimal('price', 8, 2);
$table->timestamps();
});
}
Run the migration:
Put this comment in your project terminal:
php artisan migrate
Step 3: Create a Controller
Put this comment in your project terminal:
php artisan make:controller API/ProductController --api
This will create a resource controller with methods like index
, store
, show
, update
, destroy
.
Step 4: Define API Routes
Edit routes/api.php
:
use App\Http\Controllers\API\ProductController;
Route::apiResource('products', ProductController::class);
Step 5: Implement Controller Methods
Edit app/Http/Controllers/API/ProductController.php
:
use App\Models\Product;
use Illuminate\Http\Request;
class ProductController extends Controller
{
public function index()
{
return response()->json(Product::all(), 200);
}
public function store(Request $request)
{
$validated = $request->validate([
'name' => 'required',
'description' => 'nullable',
'price' => 'required|numeric',
]);
$product = Product::create($validated);
return response()->json($product, 201);
}
public function show($id)
{
$product = Product::find($id);
if (!$product) return response()->json(['message' => 'Not Found'], 404);
return response()->json($product, 200);
}
public function update(Request $request, $id)
{
$product = Product::find($id);
if (!$product) return response()->json(['message' => 'Not Found'], 404);
$product->update($request->all());
return response()->json($product, 200);
}
public function destroy($id)
{
$product = Product::find($id);
if (!$product) return response()->json(['message' => 'Not Found'], 404);
$product->delete();
return response()->json(['message' => 'Deleted'], 200);
}
}
Don’t forget to allow mass assignment:
app/Models/Product.php
// app/Models/Product.php
protected $fillable = ['name', 'description', 'price'];
Test API Endpoints
Use Postman or CURL:
GET /api/products
– List all productsPOST /api/products
– Create productGET /api/products/{id}
– Get single productPUT /api/products/{id}
– Update productDELETE /api/products/{id}
– Delete product