V4n1ll4
V4n1ll4

Reputation: 6099

Using $input->all() instead of Input::all() Laravel-5

I'm trying to use $input->all() as opposed to Input::all() in Laravel-5, however it doesn't seem to like it, even though I am passing the Input reference to the function, like so:

/**
 * Search for a specified resource.
 *
 * @return Response
 */
public function search(Booking $booking, Input $input)
{
    dd($input->all()); // this doesn't work

    dd(Input::all()); // this DOES work

}

The error I get is:

Call to undefined method Illuminate\Support\Facades\Input::all()

Does anyone have a solution to this problem?

Upvotes: 7

Views: 17011

Answers (2)

Vinod Tigadi
Vinod Tigadi

Reputation: 859

One more important thing about using Request or Input to access the User Input is the version of Laravel that you are using.

In the Laravel 4.2 and prior, you could have access the Input::all(), Input::get() but from Laravel 5 onwards, it's been suggested to use the Input via Request facade

Ref: https://laravel.com/docs/5.2/requests

In case if you want to make use of Input in Laravel 5.0 and onwards, then you need to add this facade in the config/app.php file under the aliases section as 'Input' => Illuminate\Support\Facades\Input::class

Once you add the facade under alias, you should start using the 'Input::all()'

Hope this helps some others, who are having the confusion as whether to use 'Input' or 'Request' for Laravel 5.0 onwards.

Upvotes: 1

Francesco de Guytenaere
Francesco de Guytenaere

Reputation: 4833

I don't think you're supposed to inject Facades into your Controllers. Input is a facade for Illuminate\Http\Request and it's service container binding is request. So according to the documentation, in Laravel 5 you can do Request::all() and in Laravel 5.1 you can do $request->all()

http://laravel.com/docs/5.0/requests#retrieving-input http://laravel.com/docs/5.1/requests#retrieving-input

EDIT: This post gives some more in-depth information: https://stackoverflow.com/a/29961400/2433843

EDIT3: I think it would be great if someone could explain WHY exactly you can't inject Facades into your Controllers. I understand DI and Facades are two different things entirely, and L5+ is pushing the developers towards DI. I just don't exactly understand why injecting a facade wouldn't work, since it points towards another class, and it works when you do not inject it. Not to forget Facades and Aliases are two seperate things too. I hope someone can elaborate on this.

Upvotes: 9

Related Questions