Skip to main content

Quick Start

This guide walks you through building a complete REST API for a Product resource in under 5 minutes. By the end, you’ll have a fully functional API with filtering, pagination, and relation loading.

Prerequisites

  • PHP 8.0 or higher
  • Laravel 12.x
  • Composer

Installation

1

Install the Package

Install via Composer:
The package uses Laravel’s auto-discovery, so no need to register the service provider.
2

Publish Configuration (Optional)

If you want to customize settings, publish the config file:
This creates config/rest-generic-class.php. The package works with sensible defaults, so publishing is optional.
That’s it! The package is installed and ready to use. No migrations, no additional setup required.

Basic Setup

Let’s build a Product API with category relationships. We’ll create four files:

Step 1: Create the Model

Create app/Models/Product.php:
app/Models/Product.php
The RELATIONS constant is critical for security. Only relations listed here can be queried or loaded. This prevents attackers from accessing unintended data.

Step 2: Create the Service

Create app/Services/ProductService.php:
app/Services/ProductService.php
The service is minimal because all standard CRUD logic is inherited. You can override methods to add custom business logic when needed.

Step 3: Create the Controller

Create app/Http/Controllers/Api/ProductController.php:
app/Http/Controllers/Api/ProductController.php

Step 4: Define Routes

Add to routes/api.php:
routes/api.php
Laravel’s apiResource automatically creates these routes:
  • GET /api/v1/products → index
  • POST /api/v1/products → store
  • GET /api/v1/products/{id} → show
  • PUT /api/v1/products/{id} → update
  • DELETE /api/v1/products/{id} → destroy

Test Your API

Your API is now ready! Let’s test it:

Create a Product

Response:

List Products with Filtering

Response:

Load with Relations

Response:
This returns only products whose category name is “Electronics”.

Update a Product

Bulk Update Multiple Products

Delete a Product

What You Get Out of the Box

With these four files, you now have:

Complete CRUD

Create, read, update, and delete operations with proper HTTP methods

Dynamic Filtering

Complex queries with operators and powerful filtering capabilities

Relation Loading

Eager load relations with field selection

Pagination

Both offset-based and cursor-based pagination

Sorting

Order by any field in ascending or descending order

Bulk Operations

Update multiple records in a single request

Security

Relation allowlists, operator allowlists, query depth limits

Error Handling

Automatic database error parsing with user-friendly messages

Advanced Query Examples

Complex Filtering with Nested Logic

Translation: Get products that are in stock AND (price < 50 OR on_sale = true)

Multiple Relations with Field Selection

Loads category with only id and name, plus reviews with id, rating, comment, and the full user relation.

Nested Relation Loading

Loads category → parent and reviews → user in a single query.

Filtering on Nested Relations

Translation: Get products where the parent category’s name is “Technology”.

Cursor Pagination for Infinite Scroll

Perfect for mobile apps with infinite scrolling.

Next Steps

Now that you have a working API, explore these advanced features:

Configuration Guide

Learn about environment variables, caching, and configuration options

Basic Usage

Detailed guide to all query parameters and filtering options

Advanced Usage

Hierarchical data, role-based fields, exports, and custom logic

API Reference

Complete reference for all methods, parameters, and configurations

Common Next Tasks

Create a FormRequest class extending BaseFormRequest:
Update your controller to use it:
Add to your .env:
Cache is automatically applied to read operations and invalidated on writes.
In your model:
Now only users with the ‘admin’ role can modify these fields.
For tree structures (like categories):
Query the hierarchy:
Congratulations! You’ve built a production-ready REST API with advanced filtering, relations, and pagination in just a few minutes.

Troubleshooting

Make sure the relation is listed in your model’s RELATIONS constant:
Check your operator syntax. Format must be: field|operator|valueCorrect: "price|>=|100"
Incorrect: "price >= 100"
The package automatically includes foreign keys. Don’t worry if you specify category:id,name - the category_id FK is added automatically.
Ensure caching is enabled in config and your cache driver is properly configured:

Get Help

If you run into issues:
  1. Check the Troubleshooting Guide for common problems
  2. Review the FAQ for frequently asked questions
  3. Open an issue on GitHub
  4. Read the full Documentation for detailed information

Ready to dive deeper?

Explore the complete feature set in the Basic Usage guide