What is the difference between the http methods get, post, put, patch, and delete in laravel?

1 viewsSkills Development

What is the difference between the http methods get, post, put, patch, and delete in laravel?

Laravel’s HTTP methods (GET, POST, PUT, PATCH, DELETE) correspond directly to traditional RESTful operations and routing in routes/web.php and routes/api.php. They work in a standard HTTP fashion but Laravel offers routing helpers, form method spoofing, resource controllers, etc., to make working with them effortless.
Detailed Breakdown in Laravel
1. GET – Reading Data & Displaying Views

  • What it does: Gets resources or returns HTML views/JSON responses. Never change the state of the database.
  • Route & Controller: Typically corresponds to index or show (display one) in a controller.
  • Access in Laravel: Use $request->query(‘key’) or $request->input(‘key’) to read URL query parameters.

Route::get(‘/articles’, [ArticleController::class, ‘index’]);

Route::get(‘/articles/{article}’, [ArticleController::class, ‘show’]);

2. POST –  Creating Resources & Handling Actions

  • What it does: Provides new data to be used to create a resource, or initiates a non-idempotent action (like logging in, processing a payment, etc.).
  • Route & Controller: Typically maps to store.
  • CSRF Protection: Web routes using POST require a CSRF token (@csrf in Blade forms or an X-CSRF-TOKEN header for AJAX/SPA requests).

Route::post(‘/articles’, [ArticleController::class, ‘store’]);

3. PUT –  Full Resource Replacement

  • What it does: Replaces a whole or existing resource with the provided resource data. By the way, if the field is not provided, a strict PUT handler requires full replacement semantics—although, in Laravel apps, it’s quite common to have the same update controller method and validation rules for both PUT and PATCH

  • Route & Controller: Typically maps to update.

Route::put(‘/articles/{article}’, [ArticleController::class, ‘update’]);

4. PATCH –  Partial Resource Updates

  • What it does: Passes in part of a resource, useful if only one part of a resource is changing, e.g. is_published or status.

  • Route & Controller: Also typically maps to update (or a dedicated action).


Route::patch(‘/articles/{article}/publish’, [ArticleController::class, ‘publish’]);

5. DELETE –  Deleting Resources

  • What it does: Removes a record from the database or marks it with a timestamp when using Laravel’s SoftDeletes trait.
  • Route & Controller: Typically maps to destroy.

Route::delete(‘/articles/{article}’, [ArticleController::class, ‘destroy’]);

Important Laravel-Specific Details

  • HTML Form Limitation & Method Spoofing: Standard HTML <form> tags only natively support GET and POST. To send PUT, PATCH, or DELETE from a Blade form, use the @method directive:

<form action=”/articles/{{ $article->id }}” method=”POST”>

    @csrf

    @method(‘PATCH’)

    <button type=”submit”>Update</button>

</form>

  • Resource Controllers (Route::resource / Route::apiResource): Defining Route::resource(‘articles’, ArticleController::class) automatically registers the standard CRUD routes (GET, POST, PUT/PATCH, DELETE) across index, create, store, show, edit, update, and destroy. Use Route::apiResource() when building stateless APIs that don’t need create and edit HTML form routes.
  • Matching Any or Multiple Methods: When a route needs to respond to more than one verb, Laravel provides Route::match([‘put’, ‘patch’], …) and Route::any(…).
Abarna Vijayarathinam Asked question
0