laravel-zero / laravel-zero

A PHP framework for console artisans

Home Page:https://laravel-zero.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Using DB pagination methods inside a command throws an exception

karasibille opened this issue · comments

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 🙂

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?