Home  >  Q&A  >  body text

This route does not support the POST method. Supported methods: GET, HEAD. laravelle 9

First of all, I'm a beginner and I know this question has been asked and answered several times. I've already tried the solutions, or I've implemented the suggestions, so I'm here for expert help!

What I tried: - Added @csrf to the blade file inside my form element. - Check if posts and get in my routes are lowercase. -Change get to publish and so on fourth.

The above does not help solve the problem, alas, I am still scratching my head.

Actually, I'm trying to upload an image, add a title to the image and submit. It should go to another page showing the data in the dropdown in the upper left corner, but I see the image in the header.

Edit: Solution: I ran the following command and removed the error:

php Artisan Route: Clear

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

Wrong image occurred. Project directory structure

code show as below:

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>
@End part

Thanks in advance for your help!

P粉976737101P粉976737101229 days ago374

reply all(1)I'll reply

  • P粉495955986

    P粉4959559862024-03-28 11:46:35

    Is this caused by your route being defined as "/p/" and your form action being just "/p" (without the trailing slash)? I would first try to make sure they match exactly. Change your route to:

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

    You can also try to expand the route definition.

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

    will become

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

    reply
    0
  • Cancelreply