Laravel Request


This is an awesome feature that helps to retrieve the current HTTP application. The HTTP request method means a hypertext transfer protocol. Normally, when the client requests the server at that time, we use this method. like when you send the form or go from one page to another page at that time uses the HTTP request method.

laravel get URL in controller

You can be retrieved the current method, full URL, and path through the request method.

<?php

namespace App\Http\Controllers;
use Illuminate\Http\Request;


class UserController extends Controller {
   
   public function index(Request $request) {

      // get current method
      echo $request->url();
      //http://localhost:8000/users

      // get full Url
      echo $request->fullUrl();
      //http://localhost:8000/users?id=1&name=abc
	  
      // How to know which method use
      if($request->isMethod('GET')){
	echo "This is GET Method";
      }
      else{
	echo "This is POST Method";
      }  
   }
}

?>

laravel get request parameters in controller

You can be retrieved the data using the input() and all() method through the request method.

<?php

namespace App\Http\Controllers;
use Illuminate\Http\Request;


class UserController extends Controller {
   
   public function index(Request $request) {

     //first way
     $data = $request->input();
     
     //second way
     $data = $request->all();
     print_r($data);
     // array('id'=>1,'name'=>'abc')
   }
}

?>

laravel post request with parameters

You can be retrieved the post data using the post() method through the request method.

<?php

namespace App\Http\Controllers;
use Illuminate\Http\Request;


class UserController extends Controller {
   
   public function index(Request $request) {
      
      //Retrieve the username using post method
      $username = $request->post('username');
      echo 'Username: '.$username;
      echo '<br>';
      
      //Retrieve the password using post method
      $password = $request->post('password');
      echo 'Password: '.$password;
   }
}

?>

laravel get input from request

You can be retrieved the data using the input() method through the request method.

<?php

namespace App\Http\Controllers;
use Illuminate\Http\Request;


class UserController extends Controller {
   
   public function index(Request $request) {
      
      //Retrieve the username using input method
      $username = $request->input('username');
      echo 'Username: '.$username;
      echo '<br>';
      
      //Retrieve the password using input method
      $password = $request->input('password');
      echo 'Password: '.$password;
   }
}

?>