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!