Installation Add the package via Composer:
composer require boson-php/saucer
No service provider or facade is required—use it as a standalone utility.
First Use Case: Basic Query Building
Import the Saucer class and start building queries:
use Boson\Saucer\Saucer;
$query = Saucer::select('users.*')
->from('users')
->where('active', true)
->limit(10);
Outputs a raw SQL string (or array for parameterized queries):
SELECT users.* FROM users WHERE active = ? LIMIT 10
Where to Look First
select(), from(), where(), join(), orderBy(), limit(), groupBy().insert(), update(), delete().raw(), subquery(), union(), having().Use method chaining for fluent query building:
$query = Saucer::select('id', 'name')
->from('products')
->where('price', '>', 100)
->orderBy('name', 'asc')
->limit(5);
Leverage closures for dynamic where clauses:
$status = 'published';
$query = Saucer::select('posts.*')
->from('posts')
->where(fn($q) => $q->where('status', $status)->orWhere('draft', true));
Saucer::select('users.*', 'orders.total')
->from('users')
->leftJoin('orders', 'users.id', '=', 'orders.user_id')
->whereRaw('orders.total > 0');
$subquery = Saucer::select('user_id')->from('orders')->where('status', 'completed');
Saucer::select('users.*')
->from('users')
->whereIn('id', $subquery);
Use raw() for database-specific functions or escaping:
Saucer::select('COUNT(*) as total')
->from('users')
->whereRaw('created_at > NOW() - INTERVAL ? DAY', [30]);
Since this is a low-level package, pair it with Laravel’s DB facade for execution:
use Illuminate\Support\Facades\DB;
$query = Saucer::select('*')->from('users')->where('active', true);
$results = DB::select($query->toSql(), $query->getBindings());
Use for bulk inserts/updates:
$data = [
['name' => 'Alice', 'email' => 'alice@example.com'],
['name' => 'Bob', 'email' => 'bob@example.com'],
];
Saucer::table('users')->insert($data);
No Direct Execution
Saucer builds queries but does not execute them. Use Laravel’s DB or a PDO instance to run the query.// ❌ Won't work (no execution)
Saucer::select('*')->from('users')->get(); // Method doesn't exist
Parameter Binding Quirks
whereRaw() with placeholders (?) or bind() explicitly.// ❌ SQL Injection risk
Saucer::whereRaw("username = '$username'"); // Never do this!
Saucer::whereRaw('username = ?', [$username]);
No Eloquent Integration
Subtree Split Limitations
boson-php/boson, some features (e.g., Boson’s query caching or hydration) may not be available. Check the parent repo for context.Inspect SQL Before Execution
Use toSql() and getBindings() to debug:
$sql = Saucer::select('*')->from('users')->where('active', true)->toSql();
$bindings = Saucer::select('*')->from('users')->where('active', true)->getBindings();
dd($sql, $bindings);
Closure Scope Issues
If using closures in where(), ensure variables are accessible:
$userId = 1;
Saucer::select('*')
->from('posts')
->where(fn($q) => $q->where('user_id', $userId)); // Works
Database-Specific Syntax
Some methods (e.g., lockForUpdate()) may not be implemented. Fall back to whereRaw():
Saucer::select('*')->from('users')->whereRaw('FOR UPDATE');
Custom Query Builders
Extend Saucer by creating a wrapper class:
class CustomSaucer extends Saucer {
public function scopeActive($query) {
return $query->where('active', true);
}
}
Add Database-Specific Methods Override or extend for PostgreSQL/MySQL quirks:
Saucer::macro('jsonExtract', function ($column, $path) {
return $this->addSelect("json_extract($column, '$.$path')");
});
Integration with Query Loggers
Hook into Laravel’s query logging by tapping into toSql()/getBindings():
DB::listen(function ($query) {
if ($query->sql === Saucer::select('*')->from('users')->toSql()) {
// Log or analyze
}
});
How can I help you explore Laravel packages today?