我正在尝试发布表格,但出现错误
这是我的观点 create.blade.php
@extends('layouts.app')
@section('content')
<div class="container">
<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{{ $errors->has('caption') ? ' is-invalid' : '' }}"
name="caption"
value="{{ old('caption') }}"
autocomplete="caption" autofocus>
@if ($errors->has('caption'))
<span class="invalid-feedback" role="alert">
<strong>{{ $errors->first('caption') }}</strong>
</span>
@endif
</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">
@if ($errors->has('image'))
<strong>{{ $errors->first('image') }}</strong>
@endif
</div>
<div class="row pt-4">
<button class="btn btn-primary">Add New Post</button>
</div>
</div>
</div>
</form>
</div>
@endsection
我的路线 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');
Route::get('/profile/{user}', 'ProfilesController@index')->name('profile.show');
和我的管理员 Posts.Controller
<?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){
$data = $request->validate([
'caption' => 'required',
'image' => ['required', 'image'],
]);
Post::create($data);
dd($request->all());
}
}
这是我遇到的错误:
Symfony \组件\ HttpKernel \异常\ MethodNotAllowedHttpException此方法不支持GET方法 路线。支持的方法:POST。
答案 0 :(得分:1)
允许您路由一个名称,然后使用它
此
Route::post('/p', 'PostsController@store');
至
Route::post('/p', 'PostsController@store')->name('pstore');
添加您的路线
<form action="{{ route('pstore') }}" enctype="multipart/form-data" method="post">
答案 1 :(得分:0)
您是否可以尝试更改
<form action="/p" enctype="multipart/form-data" method="post">
进入此
<form action="{{ url('/p')} }" enctype="multipart/form-data" method="post">
或此
<form action="{{ route('PostsController@store') }}" enctype="multipart/form-data" method="post">
希望有帮助!
答案 2 :(得分:0)
为路线指定名称。而不是使用该名称来形成动作。 参见下面的代码:
路线:您可以通过两种方式编写路线。
Route::post('/p', 'PostsController@store')->name('post_store');
OR
Route::post('/p',['as' => 'post_store', 'uses' => 'PostsController@store']);
create.blade.php:
<form action="{{ route('post_store') }}" enctype="multipart/form-data" method="post">