Quick Setup
<?php
namespace App\Models;
use Ronu\RestGenericClass\Core\Models\BaseModel;
class Product extends BaseModel
{
protected $fillable = ['name', 'price', 'stock', 'category_id'];
const MODEL = 'product';
const RELATIONS = ['category', 'reviews'];
public function category()
{
return $this->belongsTo(Category::class);
}
public function reviews()
{
return $this->hasMany(Review::class);
}
}
The
RELATIONS constant is a security whitelist. Only relations listed here can be eager-loaded through the API.<?php
namespace App\Services;
use App\Models\Product;
use Ronu\RestGenericClass\Core\Services\BaseService;
class ProductService extends BaseService
{
public function __construct()
{
parent::__construct(Product::class);
}
}
<?php
namespace App\Http\Controllers\Api;
use App\Models\Product;
use App\Services\ProductService;
use Ronu\RestGenericClass\Core\Controllers\RestController;
class ProductController extends RestController
{
protected $modelClass = Product::class;
public function __construct(ProductService $service)
{
$this->service = $service;
}
}
Listing Records
Simple List
Get all products:Select Specific Fields
Reduce payload size by selecting only needed fields:Load Relations
Eager-load related data to avoid N+1 queries:The
:id,name syntax selects only specific fields from the relation. Always include foreign keys to maintain relationships.Filtering
Equality Filters (Legacy)
Filter by exact matches using theattr parameter:
Dynamic Filters with oper
Use the powerful oper parameter for complex conditions:
- Comparison:
=,!=,<,>,<=,>= - Pattern matching:
like,not like,ilike,not ilike - Set operations:
in,not in - Range:
between,not between - Null checks:
null,not null - Date:
date,not date
Combining AND and OR
Sorting
Order results by one or more fields:Pagination
Standard Pagination
Creating Records
Single Record
Bulk Create
Create multiple records in one request:The key must match the lowercase
MODEL constant defined in your model (product in this example).Updating Records
Single Update
Bulk Update
Update multiple records at once:Deleting Records
Delete by ID
Showing a Single Record
Retrieve a single record with optional relations:Common Patterns
Filtered List with Relations
Combine filtering, selection, and relation loading:Search by Name
Use thelike operator for text search:
Get Active Products in Price Range
Next Steps
Advanced Filtering
Learn complex filtering with nested relations and multiple conditions
Relation Loading
Master eager loading and nested relation queries
Caching
Configure Redis, database, or file-based caching for better performance
Bulk Operations
Optimize multiple record operations with bulk endpoints
Evidence
-
File:
src/Core/Controllers/RestController.php
Lines: 79-102, 110-121, 158-176, 186-203, 211-230, 239-243, 251-268
Shows the request parameter extraction and CRUD endpoints (index, store, update, updateMultiple, show, destroy) -
File:
src/Core/Services/BaseService.php
Lines: 414-423, 611-627, 645-662, 664-676, 1057-1069, 1071-1079
Implements list_all, create, update, update_multiple, destroy, and destroybyid methods -
File:
src/Core/Models/BaseModel.php
Lines: 28-44
Shows MODEL and RELATIONS constants used for configuration