Laravel Request::all() Should Not Be Called Statically

前端 未结 9 1504
迷失自我
迷失自我 2020-11-28 20:53

In Laravel, I\'m trying to call $input = Request::all(); on a store() method in my controller, but I\'m getting the following error:

相关标签:
9条回答
  • 2020-11-28 21:04

    The error message is due to the call not going through the Request facade.

    Change

    use Illuminate\Http\Request;
    

    To

    use Request;
    

    and it should start working.

    In the config/app.php file, you can find a list of the class aliases. There, you will see that the base class Request has been aliased to the Illuminate\Support\Facades\Request class. Because of this, to use the Request facade in a namespaced file, you need to specify to use the base class: use Request;.

    Edit

    Since this question seems to get some traffic, I wanted to update the answer a little bit since Laravel 5 was officially released.

    While the above is still technically correct and will work, the use Illuminate\Http\Request; statement is included in the new Controller template to help push developers in the direction of using dependency injection versus relying on the Facade.

    When injecting the Request object into the constructor (or methods, as available in Laravel 5), it is the Illuminate\Http\Request object that should be injected, and not the Request facade.

    So, instead of changing the Controller template to work with the Request facade, it is better recommended to work with the given Controller template and move towards using dependency injection (via constructor or methods).

    Example via method

    <?php namespace App\Http\Controllers;
    
    use App\Http\Controllers\Controller;
    use Illuminate\Http\Request;
    
    class UserController extends Controller {
    
        /**
         * Store a newly created resource in storage.
         *
         * @param  Illuminate\Http\Request  $request
         * @return Response
         */
        public function store(Request $request) {
            $name = $request->input('name');
        }
    }
    

    Example via constructor

    <?php namespace App\Http\Controllers;
    
    use App\Http\Controllers\Controller;
    use Illuminate\Http\Request;
    
    class UserController extends Controller {
    
        protected $request;
    
        public function __construct(Request $request) {
            $this->request = $request;
        }
    
        /**
         * Store a newly created resource in storage.
         *
         * @return Response
         */
        public function store() {
            $name = $this->request->input('name');
        }
    }
    
    0 讨论(0)
  • 2020-11-28 21:05

    I was facing this problem even with use Illuminate\Http\Request; line at the top of my controller. Kept pulling my hair till I realized that I was doing $request::ip() instead of $request->ip(). Can happen to you if you didn't sleep all night and are looking at the code at 6am with half-opened eyes.

    Hope this helps someone down the road.

    0 讨论(0)
  • 2020-11-28 21:06

    The facade is another Request class, access it with the full path:

    $input = \Request::all();
    

    From laravel 5 you can also access it through the request() function:

    $input = request()->all();
    
    0 讨论(0)
  • 2020-11-28 21:14
    use Illuminate\Http\Request;
    public function store(Request $request){
       dd($request->all());
    }
    

    is same in context saying

    use Request;
    public function store(){
       dd(Request::all());
    }
    
    0 讨论(0)
  • 2020-11-28 21:15

    i make it work with a scope definition

    public function pagar(\Illuminate\Http\Request $request) { //

    0 讨论(0)
  • 2020-11-28 21:17

    use the request() helper instead. You don't have to worry about use statements and thus this sort of problem wont happen again.

    $input = request()->all();
    

    simple

    0 讨论(0)
提交回复
热议问题