HTTP Request for Form Data in Laravel8

  1. Create a controller named “LoginController”
php artisan make:controller LoginController

Then create two function in LoginController

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class LoginController extends Controller
{
    public function index(){

        return view('login');
    }
    public function loginSubmit(Request $request){
        $email = $request->input('email');
        $password = $request->input('password');
        
        return "Email: " .$email ."<br> password: ".$password;
        //return $request->all();

    }
}
  1. Create a html file named “login.blade.php
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Login</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.bundle.min.js" integrity="sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM" crossorigin="anonymous"></script>

</head>
<body>
    <div class="container">
  <div class="row">
    <div class="col-md-6 offset-md-3">
        <div class="card">
            <div class="card-header">
                Login
                <div class="card-body">

                    <form method="post" action="{{route('login.submit')}}">
                        @csrf
                        <div class="mb-3">
                          <label for="email" class="form-label">Email address</label>
                          <input type="email" class="form-control" id="email" name="email">
                          
                        </div>
                        <div class="mb-3">
                          <label for="Password" class="form-label">Password</label>
                          <input type="password" class="form-control" id="password" name="password">
                        </div>
                        
                        <button type="submit" class="btn btn-primary">Submit</button>
                      </form> 

                </div>
            </div>

        </div>
    
    </div>

  </div>

</div>
</form>
</body>
</html>

Here I have used bootstrap5

3. Now write the below command in routes file

use App\Http\Controllers\LoginController;
Route::get('/login', [LoginController::class,'index'])->name('login.index');
Route::post('/login',[LoginController::class,'loginSubmit'])->name('login.submit');

4. Run the below code from command shell

php artisan serve

5. now browse http://127.0.0.1:8000/login

Leave a Reply

Your email address will not be published.