PHP code example of shammaa / laravel-optimized-queries
1. Go to this page and download the library: Download shammaa/laravel-optimized-queries library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
shammaa / laravel-optimized-queries example snippets
use Shammaa\LaravelOptimizedQueries\Traits\HasOptimizedQueries;
class Article extends Model
{
use HasOptimizedQueries;
protected $fillable = ['title', 'slug', 'content', 'user_id', 'category_id'];
}
$articles = Article::optimized()
->with('author')
->withCount('comments')
->withCount('likes')
->get();
// Result: each article has 'comments_count' and 'likes_count'
// Standard pagination (with total count)
$articles = Article::optimized()
->with('author')
->paginate(20);
// Simple pagination (faster — no count query)
$articles = Article::optimized()
->with('author')
->simplePaginate(20);
// Process large datasets in chunks
Article::optimized()
->with('author')
->where('published', true)
->chunk(500, function ($articles) {
foreach ($articles as $article) {
// process...
}
});
// Memory-efficient iteration
Article::optimized()
->with('author')
->lazy(1000)
->each(function ($article) {
// process one at a time...
});
// Load with specific locale
$articles = Article::optimized()
->with('category')
->locale('ar')
->get();
// Search in translations
$articles = Article::optimized()
->searchTranslation('لارافيل', ['title', 'content'], 'ar')
->get();
// Filter by translation
$articles = Article::optimized()
->whereTranslation('title', 'LIKE', '%Laravel%', 'en')
->get();
// Order by translation field
$articles = Article::optimized()
->orderByTranslation('title', 'asc', 'ar')
->get();
// Find by translated slug
$article = Article::optimized()
->with('author')
->whereTranslatedSlug('my-article-slug', 'en')
->first();
// Get all matching records
->get()
// Get first record
->first()
// Get first or throw 404
->firstOrFail()
// Find by ID
->find(1)
// Find by ID or throw 404
->findOrFail(1)
// Find by slug (with translations)
->findBySlug('my-article')
->findBySlugOrFail('my-article')
// Count
->count()
// Check existence
->exists()
->doesntExist()
// Get single column value
->value('title')
// Pluck column
->pluck('title')
->pluck('title', 'id')
// API-ready response
->toApi()
// See the generated SQL
$sql = Article::optimized()->with('author')->toSql();
// Dump SQL + bindings
Article::optimized()->with('author')->dump();
// Die & dump
Article::optimized()->with('author')->dd();
// Log to Laravel log
Article::optimized()->with('author')->debug()->get();
// Performance monitoring
$articles = Article::optimized()
->with('author')
->with('comments')
->get();
// Show performance after get()
Article::optimized()->with('author')->showPerformance();
use Shammaa\LaravelOptimizedQueries\Facades\OptimizedQuery;
// From model class
$articles = OptimizedQuery::from(Article::class)
->with('author')
->get();
// From existing query
$query = Article::where('published', true);
$articles = OptimizedQuery::query($query)
->with('author')
->get();
// If the optimized query fails, it silently falls back to Eloquent
// A warning is logged for debugging
$articles = Article::optimized()
->with('author')
->get(); // Always returns results, never crashes
// Disable safe mode for debugging
$articles = Article::optimized()
->with('author')
->safeMode(false)
->get(); // Will throw exception on failure
// ✅ Always paginate for lists
->paginate(20)
// ✅ Select only needed columns
->with('author', ['id', 'name'])
// ✅ Use cache for repeated queries
->cache(3600)
// ✅ Limit results
->limit(100)
// ✅ Use chunk() for background processing
->chunk(500, fn($batch) => ...)
// You request 10 relations
Product::optimized()
->with('category')
->with('brand')
->with('images')
->with('variants')
->with('reviews')
->with('tags')
->with('attributes')
->with('seller')
->withCount('orders')
->withAvg('reviews', 'rating')
->get();
// With max_relations_per_query=5, it automatically splits into:
// Query 1: base data + category + brand + images + variants + reviews
// Query 2: tags + attributes + seller (by IDs from query 1)
// Then merges the results — you don't notice any difference!