difference between laravel get and post route

13,526

It's matter of HTTP protocol. Simply said, GET is usually used for presenting/viewing something, while POST is used to change something. For example, when you fetching data for some user you use GET method and it'll look something like this:

Route::get('users/{id}', function($id) {
    $user = \App\User::find($id);

    echo "Name: " . $user->name . '<br>';
    echo "Email: " .  $user->email;
});

while with POST method you create or update user data (when user submits form you send POST request to this route):

Route::post('users', function() {
    try {
        \App\User::create([
            'name'      => \Input::get('name'),
            'email'     => \Input::get('email'),
            'password'  => bcrypt(\Input::get('password'))
        ]);

        return Redirect::intended('/');
    } catch(Exception $e) {
        return $e->getMessage();
    }
});

It's just a simple example, but I hope you can see the difference.

Share:
13,526
Admin
Author by

Admin

Updated on June 27, 2022

Comments

  • Admin
    Admin almost 2 years

    I am a beginner in laravel i am shifting from codeigniter to laravel so i dont have the concepts of routes.Can any one tell me what is the difference between a post and get route in laravel 5.

    Basic GET Route

    Route::get('/', function()
    {
        return 'Hello World';
    });
    

    Basic POST Route

    Route::post('foo/bar', function()
    {
         return 'Hello World';
    });
    

    Is their any disadvantage or benefit or if i use both of them at same time And when should i use both of them what happen if i pass parameter to them when i am using them at the same time.

    Route::match(['get', 'post'], '/', function()
    {
        return 'Hello World';
    });