If you look at the implementation of the request() helper it essentially does the same :) it resolves the request from the Container. Which is the same as using Request $request in your controller method :)
so there is no difference, use whichever is accessible to you at the moment :)
Request $request vs. request()
Hi, everyone there are two approaches to access the requested values is there anything that makes either of them a better approach?
what is the difference(s) between them?
thanks!
@rmobayen You didn't mention
Request::input
or
Request::
It's all reduced down to this: https://github.com/symfony/http-foundation/blob/master/Request.php
helpers like request() are super useful but not liked by some as it couples your code too closely to the Laravel framework. They prefer to inject all dependencies into the class.
thanks you right, that what I thought but wasnt sure!!
I wanted to know about it in general!! a) using the helper vs. b) inject the class explicitly
that is my concern. but I am not sure I understand what you are saying exactly! can you please explain a little bit more, please! or is there anywhere i can read about it?
thanks!
https://laravel.com/docs/6.x/requests
https://laravel.com/api/6.x/Illuminate/Http/Request.html
https://symfony.com/doc/current/components/http_foundation.html
https://laravel.com/docs/6.x/facades
https://laravel.com/docs/6.x/helpers
https://laravel.com/docs/6.x/helpers#method-request
https://github.com/symfony/http-foundation/blob/master/Request.php
use Illuminate\Http\Request; for
$input = $request->all();
Some "old schoolers" like requesting one item at a time.
But play around, experiment, use some trial and error.
The difference between these two is dependency injection.
As in $request, we are Injectiong the request class into our controller's function which has two benefits it creates loosely coupled code and it is also useful while writing tests for your application. While request() helper is causing tight coupling with your Laravel framework code which is not a good OOP approach.
Conclusion: Although they both do the same thing but you should prefer to use $request over request() helper as it is loosely coupled and hence facilitates writing tests.
Please or to participate in this conversation.