我正在尝试使用laravel中的表单,但不断收到此错误
Symfony \ Component \ HttpKernel \ Exception \ MethodNotAllowedHttpException 不支持 GET 方法 路线。支持的方法:开机自检。
我已经尝试了很多方法来解决它,但没有解决它
这是我的模型
create_posts_table.php
<?php
use IlluminateSupportFacadesSchema;
use IlluminateDatabaseSchemaBlueprint;
use IlluminateDatabaseMigrationsMigration;
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');
}
}
后.php
<?php
namespace App;
use IlluminateDatabaseEloquentModel;
class Post extends Model {
protected $fillable = ['caption', 'image'];
public function user(){
return $this->belongsTo(User::class);
}
}
Controller PostsController
<?php
namespace AppHttpControllers;
use IlluminateHttpRequest;
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());
}
}
路线网络.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>
似乎是正确的。你能提供你在哎呀上看到的Envoirement Details
吗!页?它们可以在右下角找到。
我假设你有一个html表单,你可以从中访问你的PostsController。您的标签应如下所示:<form action="{{route('p.store')}}" method="post"
.你可能有method="get"
.如果是这样,请将方法更改为发布,这应该有效。