PUT in laravel API

Stefan Bogdanescu

Founder & Senior Architect · 2026-06-29

Laravel Company

Mastering PUT in Laravel APIs: Solving the "Not Found" Mystery

As developers building RESTful APIs with Laravel, mastering HTTP verbs like GET, POST, PUT, and PATCH is fundamental. When you encounter issues with methods like PUT, it often stems not from a syntax error, but from a misunderstanding of how RESTful resource manipulation is intended to work within the Laravel framework.

I've seen many developers struggle with updating resources using PUT, frequently ending up with errors like "Page Not Found," even when the routes and controllers seem technically correct. This post will dissect why this happens in your specific scenario and provide the robust, idiomatic Laravel solution for implementing PUT requests effectively.

Understanding PUT vs. POST: The RESTful Distinction

Before diving into the code fix, we must establish the core principle of RESTful design. The HTTP methods define the action, and the URI defines the resource.

  • POST: Used to create a new resource. (e.g., POST /livros creates a new book).
  • PUT: Used to completely replace an existing resource at a specific URI. It requires the full representation of the resource in the request body. (e.g., PUT /livros/{id} updates the entire book identified by {id}).
  • PATCH: Used to partially update an existing resource (ideal for updating only a few fields).

Your initial setup seems to be conflating the creation logic (store) with the update logic (PUT), which is causing the routing mismatch and subsequent "Not Found" error. When you use Route::put('livro', ...) without specifying an ID, Laravel doesn't know which resource to target for the update, leading to ambiguity or failure when it tries to find the target model.

Correct Implementation for Updating Resources with PUT

To correctly implement a PUT request in Laravel, you must anchor the route to a specific resource ID. This ensures that your controller knows exactly which record to fetch and modify using Eloquent.

Step 1: Refactor Your Routes

Instead of targeting the collection endpoint (/livro) for an update, target the specific item using a route parameter. We will assume you are updating a single book.

In your routes/api.php, change the route definition to include the model ID:

// routes/api.php

Route::put('livros/{livro}', 'LivroController@update');

Notice that we changed the route from targeting the collection ('livro') to targeting a specific instance ('{livro}'). This tells Laravel that this route is designed to operate on an existing book record.

Step 2: Refactor Your Controller Logic

Your controller method must now accept the ID from the route and use it to find the model before attempting the update. We will rename the method from store to update to reflect its true purpose in RESTful design.

Here is how your LivroController.php should look:

// app/Http/Controllers/LivroController.php

use Illuminate\Http\Request;
use App\Models\Livro; // Ensure you import your model

class LivroController extends Controller
{
    public function update(Request $request, $livroId)
    {
        // 1. Find the resource based on the ID provided in the URL
        $livro = Livro::findOrFail($livroId);

        // 2. Validate and prepare the data for update
        $data = $request->validate([
            'nome' => 'required|string',
            'descricao' => 'nullable|string',
            // Add other fields you want to allow updating
        ]);

        // 3. Update the model attributes using Eloquent
        $livro->update($data);

        // 4. Return a successful response (using resources is best practice)
        return new LivroResource($livro);
    }
}

Why This Works: The Developer Perspective

The error you were seeing (Sorry, the page you are looking for could not be found) occurred because when you used Route::put('livro', 'LivroController@store'), Laravel expected the request to provide all necessary data to create a new resource (like a POST request). Since there was no ID in the URL segment, it couldn't resolve the operation correctly, resulting in a 404 error when trying to execute the logic.

By implementing the route as Route::put('livros/{livro}', ...) and using $livroId within the controller to fetch the specific record via Livro::findOrFail($livroId), you are adhering strictly to REST principles. This pattern of fetching by ID and then updating is the standard, robust way to manage resource updates in Laravel applications, aligning perfectly with best practices taught by the Laravel community.

Conclusion

Implementing HTTP verbs correctly is more about understanding architectural patterns than just writing syntax. When working with PUT in a Laravel API, always focus on identifying the specific resource you intend to modify via the URL parameters and using Eloquent's powerful find/update methods. By restructuring your routes and controller to follow RESTful conventions, you will eliminate these frustrating 404 errors and build a far more predictable and maintainable API. For deeper insights into structuring robust Laravel applications, always refer back to the official documentation at laravelcompany.com.