Laravel 9 获取错误上传表单提交到数据库并给出错误信息

Laravel 9 Getting error Upload Form Submit to Database and give an error message

我在使用 route::middleware 中的 post 方法将我的表单上传到数据库时遇到问题,并收到这样的错误 此路由不支持 POST 方法。支持的方法:GET、HEAD.

我不知道去哪里查看 again.please 帮助 me.i 已经从这里的另一个 post 找到了解决方案,但仍然没有解决。

web.php

Route::middleware(['auth', 'isAdmin'])->group(function () {
Route::get('/dashboard','App\Http\Controllers\Admin\FrontendController@index');

Route::get('categories','App\Http\Controllers\Admin\CategoryController@index');
Route::get('add-category','App\Http\Controllers\Admin\CategoryController@add');
Route::get('insert-category','App\Http\Controllers\Admin\CategoryController@insert'); });

CategoryController.php

public function insert(Request $request)
{
    $category = new Category();
    if($request->hasFile('image')->isValid())
    // {
    //     $file = $request->file('image');
    //     $ext = $file->getClientOriginalExtension();
    //     $filename = time().'.'.$ext;
    //     $file->move('assets/uploads/category',$filename);
    //     $category->image = $filename;

    // }


    $category->name = $request->input('name');
    $category->slug = $request->input('slug');
    $category->description = $request->input('description');
    $category->status = $request->input('status') == TRUE ? '1':'0';
    $category->popular = $request->input('popular') == TRUE ? '1':'0';
    $category->meta_title = $request->input('meta_title');
    $category->meta_keywords = $request->input('meta_keywords');
    $category->meta_descrip = $request->input('meta_description');
    $category->save();
    return redirect('/dashboard')->with('status',"Category Added Succesfully");
}

这是模特Category.php

class Category extends Model{
use HasFactory;

protected $table = 'categories';
protected $fillable = [
    'name',
    'slug',
    'description',
    'status',
    'popular',
    'image',
    'meta_title',
    'meta_descrip',
    'meta_keywords',
];}

这是我的表格add.blade.php

<form action="{{ url('insert-category') }}" method="POST" enctype="multipart/form-data">
            @csrf
            <div class="row">
                <div class="col-md-6 mb-3">
                    <label for="">Name</label>
                    <input type="text" class="form-control" name="name">
                </div>
                <div class="col-md-6 mb-3">
                    <label for="">Slug</label>
                    <input type="text" class="form-control" name="slug">
                </div>
                <div class="col-md-12 mb-3">
                    <label for="">Description</label>
                    <textarea name="description" rows="3" class="form-control"></textarea>
                </div>
                <div class="col-md-6 mb-3">
                    <label for="">Status</label>
                    <input type="checkbox" class="form-control" name="status">
                </div>
                <div class="col-md-6 mb-3">
                    <label for="">Popular</label>
                    <input type="checkbox" class="form-control" name="popular">
                </div>
                <div class="col-md-12 mb-3">
                    <label for="">Meta_title</label>
                    <input type="text" class="form-control" name="meta_title">
                </div>
                <div class="col-md-12 mb-3">
                    <label for="">Meta_Keyword</label>
                    <textarea name="meta_keywords" rows="3" class="form-control"></textarea>
                </div>
                <div class="col-md-12 mb-3">
                    <label for="">Meta_Description</label>
                    <textarea name="meta_description" rows="3" class="form-control"></textarea>
                </div>
                <div class="col-md-12 mb-3">
                    <input type="file" class="form-control" name="image">
                </div>
                <div class="col-md-12">
                    <button type="submit" class="form-control btn btn-primary">Submit</button>
                </div>

            </div>
    </div>
    </form>

insert-category 应该是 POST 路由而不是 GET i-e

Route::post('insert-category','App\Http\Controllers\Admin\CategoryController@insert');

使用代码:

Route::middleware(['auth', 'isAdmin'])->group(function () {
    Route::get('/dashboard','App\Http\Controllers\Admin\FrontendController@index');
    Route::get('categories','App\Http\Controllers\Admin\CategoryController@index');
    Route::get('add-category','App\Http\Controllers\Admin\CategoryController@add');
    Route::post('insert-category','App\Http\Controllers\Admin\CategoryController@insert'); 
});

我还建议使用这种结构化路由:

使用命名空间:

use App\Http\Controllers\Admin\CategoryController;

和路由:

Route::post('insert-category', [CategoryController::class, 'insert'])
    ->name('insert-category');