The POST method is not supported for this route. Supported methods: GET, HEAD. Laravel 9

Viewed 185

First off, I'm a beginner and I know this question has been asked a few times, and answered. I've attempted the solutions, or I've had the suggestion implemented, so I've come here for help from the experts!

What I've tried: -Adding @csrf to my blade file inside my form element. -Checking that my post and get's in my routes are lowercase. -Changing the gets to post and so fourth.

The above has not helped in solving the problem, and alas I'm still scratching my head.

Effectively, I'm trying to upload an image, give the image a caption and submit. It should go to another page displaying the data in a drop down in the top left hand corner, but I'm greeted with the image in the title.

EDIT: Solution: I ran the following and it removed the error:

php artisan route:clear

I've included images below of the error and my folder structure:

Image of error appearing. Project directory structure

Here's the code:

Web.php

<?php

use Illuminate\Support\Facades\Route;


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

Auth::routes();

Route::get('/p/create', 'App\Http\Controllers\PostsController@create');

Route::post('/p/', 'App\Http\Controllers\PostsController@store');

Route::get('profile/{user}', 'App\Http\Controllers\ProfilesController@index')- 
>name('profile.show');

PostsController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\User;

class PostsController extends Controller
{

public function constructor()
{
    $this->middleware['auth'];
}


public function create()
{
    return view('posts.create');
}


public function store() {
    $data = request()->validate([
    'caption' => 'required',
    'image' => 'required|image',
    ]);

    dd(request('image')->store('uploads','public'));

    auth()->user()->posts()->create($data);

    dd(request()->all());
 }
}

Model/Posts.php

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
    public function user()
    {
        return $this->belongsTo(User::class);
    }
}

views/posts/create.blade.php

@extends('layouts.app')

@section('content')

<div class="container">
    <form action="/p" enctype="multipart/form-data" method="post">
     @csrf
    <div class="row">

        <div class="col-8 offset-2">
            <div class="row mb-3">
                <div class="row pt-5">
                    <h1>Add New Post</h1>
                </div>

                <label for="caption" class="col-md-4 col-form-label">Post Caption</label>


                <input id="caption"
                       type="text"
                       class="form-control @error('caption') is-invalid @enderror"
                       name="caption"
                       value="{{ old('caption') }}"
                       autocomplete="caption" autofocus>

                @error('caption')
                        <strong>{{ $message }}</strong>
                @enderror


        </div>
        <div class="row">
            <label for="image" class="col-md-4 col-form-label">Post Image</label>
            <input type="file" class="form-control-file" id="image" name="image">

            @error('image')
            <strong>{{ $message }}</strong>
            @enderror


            <div class="pt-3">
                <button class="btn btn-primary btn-sm">Add New Post</button>
            </div>





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


</form>
@endsection

Thank you in advance for your help!

3 Answers

Could this be caused by your route being defined as "/p/", while your form action is just "/p" (without the trailing slash)? I'd try ensuring those are an exact match first. Change your route to say:

Route::post('/p', 'App\Http\Controllers\PostsController@store');

You could also try broadening that route definition.

Route::post('/p/', 'App\Http\Controllers\PostsController@store');

would become

Route::any('/p/', 'App\Http\Controllers\PostsController@store');

EDIT: Solution: I ran the following and it removed the error:

php artisan route:clear

  • you need to use and name for your router, and now route will look like

    Route::post('p',[PostsController::class, 'store'])->name('p.name');
    
  • And action form:

     <form action="{{route('p.name')}}" enctype="multipart/form-data" method="post">
    
  • And clear cache route then change:

php artisan route:cache

Related