Mann
Mann

Reputation: 606

Differentiate between GET and POST method in Laravel Controller

I have 2 routes like below,

Route::get('/','MainController@Date');
Route::post('/','MainController@Date');

or

Route::any('/','MainController@Date');

When get request is called i will calculate dates and hen post request is called i will get dates from form inputs.

when post method is called in my controler

$date1 = $request->get ( 'date1' );
$date2 = $request->get ( 'date2' );

when get is called

$date1 = will calculate using date function
$date2 = will calculate using date function

How differentiate both methods get and post, if get i should one set of things and for post another set of things

Upvotes: 1

Views: 2056

Answers (3)

Ankur Tiwari
Ankur Tiwari

Reputation: 2782

You can simply do this using below code

public function someMethod(Request $request)
{
        $method = $request->method();

        // to check if its a post method
        if ($request->isMethod('post')) {
            //
        }

        // to check if its a get method
        if ($request->isMethod('get')) {
            //
        }
}

The method method() will return the HTTP verb for the request. You may also use the isMethod method to verify that the HTTP verb matches a given string:

Upvotes: 4

the_hasanov
the_hasanov

Reputation: 843

$request->query();//return only GET param
$request->request->all()// POST param
$request->input();//all

Upvotes: 0

B.Kevin
B.Kevin

Reputation: 250

    <?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Routing\Controller;

class SmeController extends Controller
{
    /**
     * Do somthing
     * @param  Request  $request
     */
    public function update(Request $request)
    {
        if ($request->isMethod('post')) {
    //
        }
        if ($request->isMethod('get')) {
    //
        }
    }
}

you could also use $method = $request->method();

Upvotes: 1

Related Questions