In Laravel we can submit form data from 2 methods. From GET method and POST method. If we do not want to show the data in the URL, then we use the post method. When we want the functionality to search something, here we use the get method.
Some attributes are required to send post request like action attribute, method attribute. Both these attributes have to be added to the form tag. In action we pass url. Which URL should the data be sent to when the form is submitted? And in method we pass post. If we do post request without csrf token then laravel shows us error. It is necessary to send csrf token in post request. Without this, data cannot be sent through post method.
Create register form
<form action="/create-account" method="POST">
@csrf
<div class="form-group">
<label for="exampleInputEmail1">
Email address
</label>
<input aria-describedby="emailHelp" class="form-control" id="exampleInputEmail1" placeholder="Enter email" type="email">
</input>
</div>
<div class="form-group">
<label for="exampleInputPassword1">
Password
</label>
<input class="form-control" id="exampleInputPassword1" placeholder="Password" type="password">
</input>
</div>
<button class="btn btn-primary" type="submit">
Create Account
</button>
</form>
In get request we pass GET in only method. And it is not necessary to pass csrf token in this request. All the data you pass in this request will be shown in the URL. Create search form.
<form action="/search-products " method="GET">
<div class="form-group">
<label for="exampleInputEmail1">
Search
</label>
<input aria-describedby="emailHelp" class="form-control" id="exampleInputEmail1" placeholder="Search Product / Item" type="text">
</input>
</div>
<button class="btn btn-primary" type="submit">
Search Now
</button>
</form>
To get the post data in the controller, Laravel has made us request class. Whatever data we send can be accessed from the request variable.
public function create_account(Request $request){
// print all request data
$all_form_data = $request->all();
// single variable
$email = $request['email'];
$password = $request['password'];
}
To get the get method data in the controller, Laravel has made us request class. Whatever data we send can be accessed from the request variable. The data of both post and get methods can be accessed by the variable of the request.
public function search_products(Request $request)
{
// print all request data
$all_data = $request->all();
$query = $request['query'];
}
We can access the variable of the query string in the same way as we do for the data coming from the Get method.
public function product_details(Request $request){
// http://127.0.0.1:8000/product-details?title=title&id=4
$id = $request['id'];
$title = $request['title'];
}
My name is Deepak tailor as a fullstack developer. I have been in the IT industry (PHP, Nodejs, flutter) for the last 5 years. For professional and customize web development & app development, you can send inquiry on our email.
----
You can contact him at deepaktailor10@yahoo.in