Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using DB pagination methods inside a command throws an exception #478

Open
karasibille opened this issue Jan 3, 2024 · 1 comment
Open

Comments

@karasibille
Copy link

After installing the database add-on, and the illuminate/pagination package, when doing something like :

DB::connection('sqlite')->table('users')->orderBy('id')->paginate(perPage: 15, page: 1);

The paginate() call results in an exception, as we can see in the following stack trace (I'm only showing what's after the call to paginate) :

   Illuminate\Contracts\Container\BindingResolutionException 

  Target class [request] does not exist.

  at vendor/illuminate/container/Container.php:914
    910▕ 
    911▕         try {
    912▕             $reflector = new ReflectionClass($concrete);
    913▕         } catch (ReflectionException $e) {
  ➜ 914▕             throw new BindingResolutionException("Target class [$concrete] does not exist.", 0, $e);
    915▕         }
    916▕ 
    917▕         // If the type is not instantiable, the developer is attempting to resolve
    918▕         // an abstract type such as an Interface or Abstract Class and there is

  1   vendor/illuminate/container/Container.php:912
      ReflectionException::("Class "request" does not exist")

  2   vendor/illuminate/container/Container.php:912
      ReflectionClass::__construct("request")

  3   vendor/illuminate/container/Container.php:795
      Illuminate\Container\Container::build("request")

  4   vendor/laravel-zero/foundation/src/Illuminate/Foundation/Application.php:960
      Illuminate\Container\Container::resolve("request", [])

  5   vendor/illuminate/container/Container.php:731
      Illuminate\Foundation\Application::resolve("request", [])

  6   vendor/laravel-zero/foundation/src/Illuminate/Foundation/Application.php:945
      Illuminate\Container\Container::make("request", [])

  7   vendor/illuminate/container/Container.php:1454
      Illuminate\Foundation\Application::make("request")

  8   vendor/illuminate/pagination/PaginationState.php:17
      Illuminate\Container\Container::offsetGet("request")

  9   vendor/illuminate/pagination/AbstractPaginator.php:481
      Illuminate\Pagination\PaginationState::Illuminate\Pagination\{closure}()
  10  vendor/illuminate/pagination/AbstractPaginator.php:481

  11  vendor/illuminate/database/Query/Builder.php:2777
      Illuminate\Pagination\AbstractPaginator::resolveCurrentPath()

I have found a solution to fix this by extending the LaravelZero\Framework\Kernel class and overriding the $bootstrappers field like so :

    protected $bootstrappers = [
        \LaravelZero\Framework\Bootstrap\CoreBindings::class,
        \LaravelZero\Framework\Bootstrap\LoadEnvironmentVariables::class,
        \LaravelZero\Framework\Bootstrap\LoadConfiguration::class,
        \Illuminate\Foundation\Bootstrap\HandleExceptions::class,
        \LaravelZero\Framework\Bootstrap\RegisterFacades::class,
        \Illuminate\Foundation\Bootstrap\SetRequestForConsole::class, // <-- Adding this line. 
        \LaravelZero\Framework\Bootstrap\RegisterProviders::class,
        \Illuminate\Foundation\Bootstrap\BootProviders::class,
    ];

Would you consider accepting a pull request that adds this line in LaravelZero\Framework\Kernel ? If so, I would be happy to contribute 🙂

@owenvoke
Copy link
Member

owenvoke commented Jan 3, 2024

I don't see why not. 🤔 👍🏻 Feel free to PR it. 🙂 Ping @nunomaduro, do you know if there was a reason that this was excluded?

karasibille added a commit to karasibille/laravel-zero-framework that referenced this issue Jan 4, 2024
When using the Eloquent query builder pagination methods, like this :

```php
DB::table('users')->paginate(perPage: 15, page: 1);
```

Laravel tries to acces the `request` binding — and by doing so throws
this Exception :

```
Illuminate\Contracts\Container\BindingResolutionException

Target class [request] does not exist.
```

To fix this error we add the this class to the bootstrappers list of the
Kernel :

```
\Illuminate\Foundation\Bootstrap\SetRequestForConsole::class
```

Ref laravel-zero/laravel-zero#478
karasibille added a commit to karasibille/laravel-zero-framework that referenced this issue Jan 4, 2024
When using the Eloquent query builder pagination methods, like this :

```php
DB::table('users')->paginate(perPage: 15, page: 1);
```

Laravel tries to acces the `request` binding — and by doing so throws
this Exception :

```
Illuminate\Contracts\Container\BindingResolutionException

Target class [request] does not exist.
```

To fix this error, we add this class to the bootstrappers list of the
Kernel :

```
\Illuminate\Foundation\Bootstrap\SetRequestForConsole::class
```

Ref laravel-zero/laravel-zero#478
karasibille added a commit to karasibille/laravel-zero-framework that referenced this issue Jan 4, 2024
When using the Eloquent query builder pagination methods, like this :

```php
DB::table('users')->paginate(perPage: 15, page: 1);
```

Laravel tries to acces the `request` binding — and by doing so throws
this Exception :

```
Illuminate\Contracts\Container\BindingResolutionException

Target class [request] does not exist.
```

To fix this error, we add this class to the bootstrappers list of the
Kernel :

```php
\Illuminate\Foundation\Bootstrap\SetRequestForConsole::class
```

Ref laravel-zero/laravel-zero#478
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants