【发布时间】: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是从哪里来的? -
你怎么称呼邮寄路线?显示表单代码
-
请检查您的表单操作和方法。
-
这里是@Jimmyjbk 将您的表单代码更改为此
<form action="{{route('p.store')}}" enctype="multipart/form-data" method="post"> @csrf
标签: laravel