Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Fropt's avatar
Level 2

Laravel from scratch (24) PUT request not working

Hi, I'm following the new Laravel From Scratch and got stuck on Lesson 24 - PUT Requests. When I update an article I get a 404. But if I copy the generated URI and open it in a new tab it works. Also the DB won't update. My routes file: (web.php)

Route::get('/', function () {
    return view('welcome');
});

Route::get('/about', function () {
    return view('about', [
        'articles' => App\Article::take(3)->latest()->get()
    ]);
});

Route::get('/articles', 'ArticlesController@index');
Route::post('/articles', 'ArticlesController@store');
Route::get('/articles/create', 'ArticlesController@create');
Route::get('/articles/{article}', 'ArticlesController@show');
Route::get('/articles/{article}/edit', 'ArticlesController@edit');
Route::put('/articles/{article}', 'ArticlesController@update');

My ArticlesControler.php

// Persist the edited resource
    public function update($id)
    {
        $article = Article::find($id);
        
        $article->title = request('title');
        $article->excerpt = request('excerpt');
        $article->body = request('body');
        $article->save();

        return redirect('/articles/' . $article->id);
    }

My edit.blade.php

@extends('layout')

@section('head')
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/css/bulma.min.css">
@endsection

@section('content')
    <div id="wrapper">
        <div id="page" class="container">

            <h1 class="heading has-text-weight-bold is-size-4">Update Article</h1>

            <form method="POST" action="/articles/{{ $article->id }}">
                @csrf
                @method('ṔUT')

                <div class="field">
                    <label class="label" for="title">Title</label>

                    <div class="control">
                        <input class="input" type="text" id="title" name="title" value="{{ $article->title }}">
                    </div>
                </div>

                <div class="field">
                    <label for="excrept" class="label">Excerpt</label>

                    <div class="control">
                        <textarea class="textarea" id="excerpt" name="excerpt">{{ $article->excerpt }}</textarea>
                    </div>
                </div>

                <div class="field">
                    <label for="body" class="label">Body</label>

                    <div class="control">
                        <textarea class="textarea" id="body" name="body">{{ $article->body }}</textarea>
                    </div>
                </div>

                <div class="field is-grouped">
                    <div class="control">
                        <button class="button is-link" type="submit">Submit</button>
                    </div>

                </div>
            </form>
        </div>

    </div>
@endsection

Create an article works fine. The store method persists the record into the DB. But updating with the PUT method won't persist into DB and displays a 404. (e.g. http://127.0.0.1:8000/articles/7 resolves into a 404, strangely enough, if I copy and paste the URI into a new tab it will open the article ($article->id == 7) page.

I'm pretty sure this is my fault as I'm still trying to learn so please bear with me. Can someone help me understand me what am I doing wrong? Thanks!

0 likes
4 replies
Fropt's avatar
Level 2

Also, I modified the ArticlesController.php file to just die with a message:

    // Persist the edited resource
    public function update($id)
    {
        $article = Article::find($id);
        die('hello');

//        $article->title = request('title');
//        $article->excerpt = request('excerpt');
//        $article->body = request('body');
//        $article->save();
//
//        return redirect('/articles/' . $article->id);
    }

It doesn't work with the PUT method.

But if I change the method to POST or GET (both on the form and routs file, of course) it does work.

Getting very confused here...

andyandy's avatar
andyandy
Best Answer
Level 4

I'm new and I'm have no idea what I'm talking about. But to update I'm using PATCH, not PUT.

Fropt's avatar
Level 2

@andyandy It works! I've read some discussions about PATCH and PUT, and I heard Jeffrey Way saying there's no big difference. I really don't understand the difference or why PUT didn't work with me, I think it should. Anyway I'm voting your answer as the best answer :) Thanks again

1 like

Please or to participate in this conversation.