【问题标题】:How to debug this error 'The GET method is not supported for this route. Supported methods: POST.'?如何调试此错误“此路由不支持 GET 方法。支持的方法:POST。'?
【发布时间】:2019-12-27 12:32:44
【问题描述】:

我正在尝试在 laravel 中使用表单,但我不断收到此错误

Symfony\Component\HttpKernel\Exception\ MethodNotAllowedHttpException 不支持 GET 方法 路线。支持的方法:POST。

我尝试了很多方法来解决它,但没有解决它

这是我的模型

create_posts_table.php

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreatePostsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('posts', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->unsignedBigInteger('user_id');
            $table->string('caption');
            $table->string('image');
            $table->timestamps();

            $table->index('user_id');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('posts');
    }
}

post.php

<?php
namespace App;
use Illuminate\Database\Eloquent\Model;

class Post extends Model {
    protected $fillable = ['caption', 'image'];

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

Controller PostsController

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class PostsController extends Controller
{
    public function create(){
        return view('posts.create');
    }


    public function store(Request $request){
        $request->validate([
            'caption' => 'required',
            'image' => 'required|image|mimes:jpeg,png,jpg,gif,svg',
        ]);

        Post::create($request->input());

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

路由 web.php

<?php

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
j|
*/

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

Auth::routes();

Route::get('/p/create', 'PostsController@create');
Route::post('/p', 'PostsController@store')->name('p.store');

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

刀片文件:

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

请帮助我,由于上述错误,我已经被堆栈 3 天了。

在浏览器中我有这个

 protected function methodNotAllowed(array $others, $method)
    {
        throw new MethodNotAllowedHttpException(
            $others,
            sprintf(
                'The %s method is not supported for this route. Supported methods: %s.',
                $method,
                implode(', ', $others)
            )
        );
    }

【问题讨论】:

  • 你的form是从哪里来的?
  • 你怎么称呼邮寄路线?显示表单代码
  • 请检查您的表单操作和方法。
  • 这里是
    @csrf
  • @Jimmyjbk 将您的表单代码更改为此 &lt;form action="{{route('p.store')}}" enctype="multipart/form-data" method="post"&gt; @csrf

标签: laravel


【解决方案1】:

编辑:根据您的评论,您的&lt;form&gt; 似乎是正确的。您能否提供您在Whoops 上看到的Envoirement Details!页?它们位于右下角。

我假设您有一个 html 表单,您可以从中访问 PostsController。您的标签应如下所示:&lt;form action="{{route('p.store')}}" method="post"。你可能有method="get"。如果是这样,将方法更改为发布,应该可以。

【讨论】:

  • 这里在 whoops 页面“/Users/macair13/freeCodeGram/vendor/laravel/framework/src/Illuminate/Routing/RouteCollection.php”
  • 我拥有你告诉我的一切
猜你喜欢
相关资源
最近更新 更多
热门标签