Laravel: The POST method is not supported for this route

Viewed 298

I'm kinda new to Laravel and I made a form for my page which users can add new image, and this form is in create.blade.php:

<form action="/p" enctype="multipart/form-data" method="post">
@csrf
<div class="row">
    <div class="col-8 offset-2">
        <div class="row">
            <h1>Add New Post</h1>
        </div>
        <div class="form-group row">
            <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')
                    <span class="invalid-feedback" role="alert">
                        <strong>{{ $message }}</strong>
                    </span>
                @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')
                <span class="invalid-feedback" role="alert">
                    <strong>{{ $message }}</strong>
                </span>
            @enderror
        </div>
        <div class="row pt-4">
            <button class="btn btn-primary">Add New Post</button>
        </div>
    </div>
</div>

And this is the web.php (routes) file:

Route::get('/p/create','PostsController@create');
Route::get('/p','PostsController@store');

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

As you can see it refers to PostController.php which is this:

class PostsController extends Controller
{
    public function create()
    {
        return view('posts.create');
    }
    public function store()
    {
        dd(request()->all());
    }
}

Also I do the command php artisan route:list and this is it:

print screen

So what is going wrong here? I've searched a lot but couldn't find anything useful. So if you know how to solve this, please let me know.

Thanks in advance

3 Answers

You are sending the request to the server so you need to set your HTTP request as post not get, Like this

Route::post('/p','PostsController@store');

You need to add a POST route

Route::post('/p','PostsController@store');
  1. In create.blade.php form method is POST but in web.php is Route::get('/p','PostsController@store'); so you should change Route::post('/p','PostsController@store') instead of Route::get('/p','PostsController@store')

  2. In Controller

    use Illuminate\Http\Request;
    class PostsController extends Controller
    {
        public function create()
        {
            return view('posts.create');
        }
        public function store(Request $request)
        {
            dd($request->input('image'));
        }
    }
    
Related