phplaravelroutescontrollers

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


Solution

  •     <?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();