LARAVEL Tutorial


Laravel 7 Request Tutorial


The HTTP Request Communicate Between routes and Controller in Laravel. The Illuminate \ Http \ Request example provides several ways to check HTTP requests on your web application.

Accessing The Request

If you want to access the request object in your web application controller. You need to place the Illuminate\ Http\Request class on your controller constructor or method.

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class MyController extends Controller
{
    /**
     * Store a new user.
     *
     * @param  Request  $request
     * @return Response
     */
    public function store(Request $request)
    {
        $email = $request->input('email');

        //
    }
}

Accessing The Request With Route Parameters

If you want to access the request object in your web application controller with route parameters. You also need to place the Illuminate\ Http\Request class on your controller constructor or method.

You get your route parameter id by defining your controller method like the following:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class MyController extends Controller
{
    /**
     * Update the specified user.
     *
     * @param  Request  $request
     * @param  int  $id
     * @return Response
     */
    public function update(Request $request, $id)
    {
        //laravel access request with parameters
    }
}

Accessing The Request on Routes

If you want to access the request object in your web application routes. You also need to place the Illuminate\ Http\Request class on your routes.

You get or access request object in your web application route, like the following:

use Illuminate\Http\Request;

Route::get('/post', function (Request $request) {

   //access request object

});

Get Request URL/URI

To get/retrieve request URI/URL in your controller, you can use request->path() method for that. You could do like below:

$uri = $request->path();

The path method returns the request's URL/URI. So, if your request incoming from http://w3alert.com/laravel/tutorial, this method will return laravel/tutorial:

To get the full request URL, you may use the URL method on the request instance:

$url = $request->url();

Get the Request Method

To get/retrieve the request method, you can use request->method() method for that. You could do like below:

$reqMethod = $request->method();

dd($reqMethod);

laravel Get Request Input

To get/retrieve the inputs from HTTP request, you can use $request->input(), $request->input_parameter_name, $reqest->get(), $request->all() method for that. You could do like below:

//get single inputs
$email = $request->input('email');

$email = $request->email;

//get all inputs

$all = $request->all();

$all = $request->get();

laravel Get Request Cookies

To get the cookies to value from the request, you can use $request->cookies() method for that. You could do like below:

$value = $request->cookie('name');

To Get/retrieve File from request

To Get/retrieve File from HTTP request, you can use $request->cookies() method for that. You could do like below:

$file = $request->file('image');

Check file exist

You can also check if a file exists on the HTTP request or not. You can use the hasFile method:

if ($request->hasFile('image')) {
    //
}

Save or Store file in Folder

To save/store file in folder, you can use the laravel move method for that. Basically this method will store the file from the folder. You could like below:

$request->file('image')->move($destinationPath);

$request->file('image')->move($destinationPath, $fileName);