Laravel Http Request

Manish Chaudhary
ISOP Nepal
Published in
2 min readMay 6, 2020

In this post we will know about HTTP request in laravel. We will see how we can obtain details about request like request method, request url, request data if we are submitting a form and many more.

We can obtain the details of request using Illuminate\Http\Request class. For that we will have to type hint this class on controller method like you see in below code snippet.

use Illuminate\Http\Request;class TodoController extends Controller
{
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
//
}
}

We should not forget to import the Illuminate\Http\Request class.

We can inject the class in route closure like this.

use Illuminate\Http\Request;Route::get('/', function (Request $request) {
//
});

Then we can get the request url without query string like this.

$url = $request->url();

To get full url with query strings we can use this snippet.

$url = $request->fullUrl();

To get the request method we can use this snippet.

$method = $request->method();

To get all the input data submitted from a form or an api we can use this snippet.

$input = $request->all();

To get specific input data we use below snippet.

$name = $request->input('name');
// or
$name = $request->name;

To get the query parameter we use below snippet.

$name = $request->query('name');

To get only certain input data we use only like this.

$data= $request->only(['email', 'password']);

To omit certain input data we use except like this.

$data= $request->except(['password']);

To check if input data contains certain input value we use below snippet.

if ($request->has('email')) {
//
}

That’s all in this post. for more details about the HTTP requests check this link.

--

--

Manish Chaudhary
ISOP Nepal

Software developer and father to a beautiful daughter