Skip to main content
Find answers to commonly asked questions about using Rest Generic Class in your Laravel applications.

General Usage

No. Rest Generic Class does not automatically register routes. You maintain full control over your routing.You register routes in your Laravel application’s route files and wire them to controllers that extend RestController:
use App\Http\Controllers\Api\ProductController;

Route::prefix('v1')->group(function () {
    Route::apiResource('products', ProductController::class);
    Route::post('products/update-multiple', 
        [ProductController::class, 'updateMultiple']);
});
This gives you complete flexibility over URL structure, middleware, and route organization.
Yes! The package includes a BaseModelMongo class specifically for MongoDB usage through the mongodb/laravel package.
use Ronu\RestGenericClass\Core\Models\BaseModelMongo;

class Product extends BaseModelMongo
{
    protected $connection = 'mongodb';
    const MODEL = 'product';
    const RELATIONS = ['category', 'reviews'];
}
You are responsible for installing and configuring mongodb/laravel in your application. The package provides the base model but doesn’t include MongoDB dependencies.
No. Spatie’s laravel-permission package is completely optional.The permission models, traits, and middleware are available if you choose to install spatie/laravel-permission, but the core functionality works without it.
# Optional - only install if you need permission management
composer require spatie/laravel-permission
If you don’t install Spatie, you can still use the package’s core CRUD, filtering, and relation-loading features.
Yes! Hierarchy support is built-in when your model defines the HIERARCHY_FIELD_ID constant and you pass the hierarchy parameter in requests.
class Category extends BaseModel
{
    const HIERARCHY_FIELD_ID = 'parent_id';
}
Request example:
{
  "hierarchy": {
    "filter_mode": "with_descendants",
    "children_key": "children",
    "max_depth": 3
  }
}
See the Hierarchy documentation for all available modes and options.

Performance

Rest Generic Class supports generic cache integration via Laravel’s cache stores (Redis, database, file, Memcached, etc.).Enable caching:
REST_CACHE_ENABLED=true
REST_CACHE_STORE=redis
REST_CACHE_TTL=60
Key features:
  • Cache is applied to list_all and get_one operations
  • Cache keys include model, query params, auth user, and selected headers
  • Write operations automatically bump a model-level cache version to invalidate stale data
  • Per-request control via cache=false or cache_ttl=120 parameters
Multi-tenant aware: Cache keys vary by Accept-Language and X-Tenant-Id headers to prevent cross-tenant data leaks.See the Cache strategy for complete caching options.
The package includes built-in safety limits to protect your database:
LimitDefaultConfig Key
Maximum nesting depth5filtering.max_depth
Maximum conditions100filtering.max_conditions
These limits prevent abusive queries with excessive complexity.You can adjust them in config/rest-generic-class.php:
'filtering' => [
    'max_depth' => 10,
    'max_conditions' => 200,
],
Only increase these limits if you have legitimate use cases. Higher limits can impact database performance.
Use selective field loading to reduce data transfer and improve performance:
GET /api/v1/products?select=["id","name","price"]&relations=["category:id,name","reviews:id,rating"]
Best practices:
  • Only load relations you need
  • Use field selection (:id,name) on relations to limit columns
  • Enable caching for frequently accessed data
  • Set appropriate cache TTLs based on data volatility
  • Use pagination for large result sets

Configuration

The package uses a single cache store configured via REST_CACHE_STORE.However, you can:
  • Set different TTLs for list vs single record operations:
    REST_CACHE_TTL_LIST=120
    REST_CACHE_TTL_ONE=300
    
  • Override TTL per request:
    GET /api/v1/products?cache_ttl=600
    
  • Disable cache per request:
    GET /api/v1/products?cache=false
    
The cache version is tracked per model, so updates to one model won’t invalidate cache for other models.
Enable query logging in your environment:
LOG_QUERY=true
Queries will be logged to storage/logs/query.log.You can also configure the general log level:
LOG_LEVEL=debug
Query logging can significantly increase log file size. Use it for debugging and disable in production unless actively troubleshooting.
If you’re using Laravel’s config caching (common in production), environment variables are cached.After changing .env, clear the config cache:
php artisan config:clear
Then rebuild it:
php artisan config:cache
The package is fully compatible with Laravel’s config caching because all environment variables are only referenced in the config file, not directly in code.

Development

The package does not ship with automated tests.Validation should be performed in your host application with tests covering:
  • Feature tests for CRUD endpoints using your RestController subclasses
  • Tests for oper filtering and relation allowlist enforcement
  • Tests for hierarchy listing if you use HIERARCHY_FIELD_ID
  • Authorization tests if using Spatie permissions
This approach ensures tests are relevant to your specific implementation and use cases.
Absolutely! The package is designed for extension:
// Extend the base service
class ProductService extends BaseService
{
    public function __construct()
    {
        parent::__construct(Product::class);
    }

    // Add custom methods
    public function getLowStock($threshold = 10)
    {
        return $this->model->where('stock', '<', $threshold)->get();
    }
}

// Extend the controller
class ProductController extends RestController
{
    // Override methods or add new endpoints
    public function lowStock(Request $request)
    {
        return response()->json(
            $this->service->getLowStock($request->input('threshold', 10))
        );
    }
}
You have complete control over extending functionality while benefiting from the base features.
Contributions are welcome!
  1. Open issues on GitHub for bugs or feature requests
  2. Submit pull requests with improvements
  3. For security concerns, report them privately to the maintainer
See the Contributing guide for detailed guidelines.

Need More Help?

Still have questions?

Build docs developers (and LLMs) love