Laravel Request: method()

Defination

This function used to get the request method.

Syntax

public function method():string

Usecases

  • In scenario, when you want to check which type of method is used in the current request.

Example-1: Use of method function in Laravel Controller

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class UserController extends Controller
{
    public function index(Request $request)
    {
        dd($request->method()); // output:- GET
    }
}

Example-2: Use of method function when you don't have Request object

Following example can be used anywhere in Laravel app:

namespace App\Helpers;

use Illuminate\Http\Request;

class Test 
{
    public function __construct() 
    {
        dd(request()->method()); // output:- GET
    }
}