Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 463
V4n1ll4 Avatar asked Jun 30 '15 15:06

V4n1ll4


People also ask

How to take input in Laravel?

Getting Only Some Of The Request Input$input = Input::only('username', 'password'); $input = Input::except('credit_card'); When working on forms with "array" inputs, you may use dot notation to access the arrays: $input = Input::get('products.0.name');

What is request () in laravel?

Introduction. Laravel's Illuminate\Http\Request class provides an object-oriented way to interact with the current HTTP request being handled by your application as well as retrieve the input, cookies, and files that were submitted with the request.

Which method allows you to verify that the incoming request path matches a given pattern in laravel?

The “path” method is used to retrieve the requested URI. The is method is used to retrieve the requested URI which matches the particular pattern specified in the argument of the method.

Does laravel have request?

The has MethodThe $request->has() method will now return true even if the input value is an empty string or null . A new $request->filled() method has been added that provides the previous behaviour of the has() method. The $request->exists() method still works, it is just an alias for $request->has() .


2 Answers

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.

like image 81
Francesco de Guytenaere Avatar answered Sep 19 '22 13:09

Francesco de Guytenaere


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.

like image 32
Vinod Tigadi Avatar answered Sep 20 '22 13:09

Vinod Tigadi