- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to get the contents of the HTTP Request body in Laravel?
To get the details of the HTTP request you need to make use of the class Illuminate\Http\Request.
With the above class, you will be able to fetch the input, cookies, and files from HTTP requests. Now consider the following form –
To get all the details from the HTTP request you can do as follows −
Example 1
Using $request->all() method
Enter the details in the form as shown below −
Once you submit it will retrieve all the input data and return an array with data.
public function validateform(Request $request) { $input = $request->all(); print_r($input); }
Output
The output of the above code is −
Array ( [_token] => 367OQ9dozmWlnhu6sSs9IvHN7XWa6YKpSnnWrBXx [name] => Rasika Desai [email] => rasika@gmail.com [age] => 20 [address] => Pune )
Example 2
Using $request->collect() method.
This method will return the data as a collection.
public function validateform(Request $request) { $input = $request->collect(); print_r($input); }
Output
The output of the above code is −
Illuminate\Support\Collection Object ( [items:protected] => Array( [_token] => 367OQ9dozmWlnhu6sSs9IvHN7XWa6YKpSnnWrBXx [name] => Rasika Desai [email] => rasika@gmail.com [age] => 20 [address] => Pune ) [escapeWhenCastingToString:protected] => )
Example 3
Using $request->getContent() method.
This method gives output as a URL query string, the data being passed in the key/value pair.
public function validateform(Request $request) { $input = $request->getContent(); echo $input; }
Output
The output of the above code is
_token=367OQ9dozmWlnhu6sSs9IvHN7XWa6YKpSnnWrBXx&name=Rasika+Desai&email=rasika%40gmail.com&age=20&address=Pune
Example 4
Using php://input
This will return the data coming from the input field in the URL query string.
$data = file_get_contents('php://input'); print_r($data);
Output
The output of the above code is −
_token=367OQ9dozmWlnhu6sSs9IvHN7XWa6YKpSnnWrBXx&name=Rasika+Desai&email=rasika%40gmail.com&age=20&address=Pune