- 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 extract raw form data in Laravel?
To demonstrate the above, we are going to make use of the below form It has username and password fields. Let us try to get raw form data in the methods shown below −
Example 1
Using file_get_contents()
file_get_contents() method is a built-in PHP function and it returns the file content into a string format. Now to get raw data of the form you can give the input to the file_get_contents as php://input
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; class testvalidationController extends Controller { public function showform() { return view('login'); } public function validateform(Request $request) { $test = file_get_contents('php://input'); echo $test; } }
Output
The output of the above code is −
_token=zHuIkXpqcRqvZO4vTgxH0fFk5fCmvqSavrCjHVMi&username=testing&password=abcd
Example 2
Using getContent() method.
The getContent() can be used on the Request class and it will return the form data in string format.
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use Illuminate\Support\Facades\Input; class testvalidationController extends Controller { public function showform() { return view('login'); } public function validateform(Request $request) { echo Request::createFromGlobals()->getContent(); } }
Output
The output of the above code is −
_token=zHuIkXpqcRqvZO4vTgxH0fFk5fCmvqSavrCjHVMi&username=test&password=xya
Example 3
Using all() method on Request class
The all() method will return all the form data in an array.
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use Illuminate\Support\Facades\Input; class testvalidationController extends Controller { public function showform() { return view('login'); } public function validateform(Request $request) { $data = Request::createFromGlobals()->all(); print_r($data); } }
Output
The output of the above code is −
Array ( [_token] => zHuIkXpqcRqvZO4vTgxH0fFk5fCmvqSavrCjHVMi [username] => testing [password] => xyz )
Example 4
Using get() method
You can also make use of the get() method available with the Request class. You need to pass the field name of which you need to read the value.
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use Illuminate\Support\Facades\Input; class testvalidationController extends Controller { public function showform() { return view('login'); } public function validateform(Request $request) { echo $username = Request::createFromGlobals()->get('username'); echo "<br/>"; echo $password = Request::createFromGlobals()->get('password'); } }
Output
The output of the above code is −
testing sdsads