If you’ve ever stared at a tangled mess of DB::raw() statements or nested subqueries and thought, “There has to be a better way”—you’re not alone. Laravel’s Query Builder is powerful, but when it comes to writing expressive, dynamic SQL logic, things can get messy fast.
Enter Laravel Query Enrich: a package designed to solve the pain points of writing complex queries while keeping your code readable, maintainable, and database-agnostic.
🔧 Problem 1: Conditional Logic Without Raw SQL
Scenario: You want to categorize products based on price ranges—cheap, moderate, expensive—but don’t want to write raw SQL.
Traditional Approach:
DB::raw("CASE WHEN price > 100 THEN 'expensive' WHEN price BETWEEN 50 AND 100 THEN 'moderate' ELSE 'cheap' END AS price_category")
Query Enrich Solution:
QE::case()
->when(c('price'), '>', 100)->then('expensive')
->when(QE::condition(50, '<', c('price')), QE::condition(c('price'), '<=', 100))->then('moderate')
->else('cheap')
->as('price_category')
✅ No raw SQL. ✅ Fully readable. ✅ Easy to tweak.
🔧 Problem 2: Date-Based Filtering That Works Across Databases
Scenario: You need to fetch records created in the last 7 days, but want to avoid database-specific syntax.
Traditional Approach:
DB::raw("created_at >= DATE_SUB(NOW(), INTERVAL 7 DAY)")
Query Enrich Solution:
->where(c('created_at'), '>=', QE::subDate(QE::now(), 7, Unit::DAY))
✅ Works across MySQL, PostgreSQL, etc. ✅ No vendor lock-in.
🔧 Problem 3: Subqueries That Don’t Look Like a Crime Scene
Scenario: You want to check if an author has written any books using an EXISTS clause.
Traditional Approach:
DB::raw("EXISTS (SELECT 1 FROM books WHERE books.author_id = authors.id)")
Query Enrich Solution:
QE::exists(
DB::table('books')->where('books.author_id', c('authors.id'))
)->as('has_books')
✅ Clean syntax. ✅ Easy to debug. ✅ No raw SQL.
🔧 Problem 4: Concatenating Fields Without Losing Your Mind
Scenario: You want to display full names by combining first and last names.
Traditional Approach:
DB::raw("CONCAT_WS(' ', first_name, last_name) AS full_name")
Query Enrich Solution:
QE::concatWS(' ', c('first_name'), c('last_name'))->as('full_name')
✅ No string gymnastics. ✅ Fully readable.
🔧 Problem 5: Complex Nested Conditions That Don’t Break Your Brain
Scenario: You need to build a query with multiple nested conditions, like filtering users based on age, role, and activity status.
Query Enrich makes it easy:
QE::condition(
QE::condition(c('age'), '>', 18),
QE::condition(c('role'), '=', 'admin'),
QE::condition(c('is_active'), '=', true)
)
✅ Logical grouping. ✅ No parentheses chaos.
🧪 Bonus: Dynamic Query Composition
You can build reusable query fragments and compose them dynamically—perfect for dashboards, filters, and reports.
📝 Final Thoughts
Laravel Query Enrich isn’t just a helper—it’s a mindset shift. It lets you write expressive, readable, and powerful queries without sacrificing clarity or maintainability. Whether you’re building analytics dashboards, filtering data-heavy tables, or just trying to avoid raw SQL, this package is a must-have.
Fuel my creative spark with a virtual coffee! Your support keeps the ideas percolating—grab me a cup at Buy Me a Coffee and let’s keep the magic brewing!
