我正在尝试使用laravel中的表单,但我不断收到此错误
Symfony \组件\ HttpKernel \异常\ 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)
)
);
}
答案 0 :(得分:0)
编辑:根据您的评论,您的<form>
似乎是正确的。您能否提供您在Whoops上看到的Envoirement Details
!页?可以在右下角找到它们。
我假设您有一个HTML表单,从中可以访问PostsController。您的标签应如下所示:<form action="{{route('p.store')}}" method="post"
。您可能有method="get"
。如果是这样,将方法更改为发布,那应该可以。