How can I query raw via Eloquent?

How can I query raw via Eloquent?

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Mixing Eloquent and Raw SQL in Laravel

When you find yourself needing to execute a raw SQL query within an application that otherwise relies heavily on Eloquent, the immediate instinct is often to look for a helper method that wraps the raw string. You might recall seeing patterns like Model::query($sql) in older documentation or third-party packages and assume it's a built-in feature of modern Laravel. However, this specific syntax does not exist in the standard framework. Attempting to pass a SQL string directly to the model class will result in an error because Eloquent models are designed to interpret binding parameters and relationships, not to execute arbitrary SQL strings as primary commands.

To bridge the gap between structured Eloquent queries and the flexibility of raw SQL, you must use the DB facade or the model's underlying query builder instance. The most direct approach is accessing the database connection via the DB facade. This allows you to construct a raw string while still leveraging Laravel's parameter binding system for security.

use Illuminate\Support\Facades\DB;

// Executing a raw query within an application context
$users = DB::select('SELECT * FROM users WHERE id IN (?, ?)', [$id1, $id2]);

This pattern ensures that your SQL remains readable while preventing SQL injection attacks through proper binding. The DB facade is the standard entry point for these operations and works consistently across all versions of Laravel without requiring additional packages.

If your goal is to integrate this raw logic into a specific model class, you can access the underlying query builder or connection from within the model itself. This approach keeps the code scoped to the entity you are working with. You can inject the database connection into the model's constructor or use the static use statement to access the global instance.

class User {
    public function findActiveUsers()
    {
        $rawQuery = "SELECT * FROM users WHERE status = 'active' AND created_at > NOW() - INTERVAL ? DAY";
        
        // Accessing the raw query builder from the model
        $query = static::connection()->query($rawQuery);
        
        return json_decode($query->executeToArray(), true);
    }
}

In this example, we use the connection() helper on the model to get the database connection instance. Calling query() on that connection executes the raw SQL string immediately. Note that the return type here is a result set object, which you then convert into an array using executeToArray(). This is distinct from Eloquent's eager loading or relationship methods, as it bypasses those entirely.

A critical trade-off to consider when choosing this path is data binding. While the DB facade handles bindings seamlessly, raw queries executed via connection()->query() often require you to handle result sets manually. You lose the convenience of accessing attributes as properties (e.g., $user->name) unless you explicitly map the results back into an Eloquent model or a plain PHP array.

Another common pitfall involves scope definitions. If your application relies on global scopes defined in the model, executing raw SQL completely bypasses those scopes. This can lead to discrepancies between what you expect based on your model's configuration and what the database actually returns. For instance, if you have a soft delete scope active on the User model, a raw query against the users table will not automatically exclude deleted records unless you add that logic manually to your SQL string.

If you need more control over the query structure but still want to benefit from Eloquent's relationship definitions or scopes, consider using the Query Builder directly through the model without casting it to a full model instance. This allows you to chain methods like where, orderBy, and select before finally executing the raw SQL if necessary, though typically, once you switch to raw strings, you step out of the Eloquent abstraction layer entirely.

For complex reporting requirements where performance is paramount and the schema is well-understood, raw SQL via the DB facade or model connection remains a valid strategy. It offers the flexibility needed for advanced data manipulation while keeping your application secure through Laravel's binding mechanisms.

Our network