Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

prasadchinwal5's avatar

Disable `SELECT TOP 1` query from laravel eloquent

I am using a VQL data source connection. I can use the DB queries to fetch the data ex. DB::table('users')->get(); but when I use eloquent User::get() I get an error since Laravel runs SELECT TOP 1 * FROM USERS and this is not supported in VQL.

VQL supports SELECT * FROM USERS LIMIT 1. How can I overcome this issue?

0 likes
1 reply
LaryAI's avatar
Level 58

To address the issue of Laravel Eloquent generating SELECT TOP 1 instead of SELECT * FROM USERS LIMIT 1, you can customize the query grammar for your database connection. Laravel allows you to extend or replace the default query grammar to suit your needs.

Here's a step-by-step solution to override the default behavior:

  1. Create a Custom Query Grammar:

    First, create a custom query grammar class that extends the default grammar and overrides the compileSelect method to use LIMIT instead of TOP.

    namespace App\Database\Query\Grammars;
    
    use Illuminate\Database\Query\Grammars\SqlServerGrammar;
    
    class CustomGrammar extends SqlServerGrammar
    {
        /**
         * Compile a select query into SQL.
         *
         * @param  \Illuminate\Database\Query\Builder  $query
         * @return string
         */
        public function compileSelect(\Illuminate\Database\Query\Builder $query)
        {
            if ($query->limit > 0) {
                $query->orders = $query->orders ?: $this->defaultOrderings($query);
            }
    
            return parent::compileSelect($query);
        }
    
        /**
         * Compile the "limit" portions of the query.
         *
         * @param  \Illuminate\Database\Query\Builder  $query
         * @return string
         */
        protected function compileLimit(\Illuminate\Database\Query\Builder $query, $limit)
        {
            return 'LIMIT ' . (int) $limit;
        }
    }
    
  2. Register the Custom Grammar:

    Next, you need to register this custom grammar in your database service provider. If you don't have a custom service provider, you can create one using the following command:

    php artisan make:provider CustomDatabaseServiceProvider
    

    Then, in your CustomDatabaseServiceProvider, register the custom grammar:

    namespace App\Providers;
    
    use Illuminate\Support\ServiceProvider;
    use Illuminate\Database\Connection;
    use App\Database\Query\Grammars\CustomGrammar;
    
    class CustomDatabaseServiceProvider extends ServiceProvider
    {
        /**
         * Register any application services.
         *
         * @return void
         */
        public function register()
        {
            // Override the default query grammar for the connection
            Connection::resolverFor('sqlsrv', function ($connection, $database, $prefix, $config) {
                $connection = new \Illuminate\Database\SqlServerConnection($connection, $database, $prefix, $config);
                $connection->setQueryGrammar(new CustomGrammar);
                return $connection;
            });
        }
    }
    
  3. Register the Service Provider:

    Finally, register your custom service provider in the config/app.php file:

    'providers' => [
        // Other Service Providers
    
        App\Providers\CustomDatabaseServiceProvider::class,
    ],
    

By following these steps, you will override the default behavior of Laravel Eloquent to use LIMIT instead of TOP for your queries, which should be compatible with your VQL data source.

This solution ensures that all Eloquent queries will use the LIMIT clause, making them compatible with your VQL data source.

Please or to participate in this conversation.