简体   繁体   English

在 laravel 8 中使用 POST 将数据插入数据库时出现问题

[英]Problem in inserting data to database using POST in laravel 8

I am having trouble inserting data from the form that I made into the database.我无法将我制作的表单中的数据插入数据库。 I am using the POST method and I have made sure that I have done everything correctly.我正在使用 POST 方法,并确保我已正确完成所有操作。 I need advice and help in this part because I have been stuck in this part for hours.我在这部分需要建议和帮助,因为我已经被困在这部分好几个小时了。

Below is the code of my controller:下面是我的 controller 的代码:

public function insertProduct(Request $request){
        $rules = [
            'name' => 'required|string|min:5',
            'description'=>'required|string|min:15|max:500',
            'price'=>'required|numeric|min:1000|max:10000000',
            'stock'=>'required|numeric|min:1|max:10000',
            'image'=>'required|image|mimes:jpg,png,jpeg',
            'category'=>'required'
        ];

        $validator = Validator::make($request->all(), $rules);

        if($validator->fails()){
            return back()->withErrors($validator);
        }

        $products = new Products();
        $products->name = $request->name;
        $products->description = $request->description;
        $products->price = $request->price;
        $products->category_id = $request->categories;
        $file = $request->file('image');
        $fileName = time().$file->getClientOriginalName();
        Storage::putFileAs('public/images', $file, $fileName);
        $products->image='images/'.$fileName;

        $products->save();

   
        return redirect('/insertProducts', ["products"=>Products::all()]);
    }

Below is the code of my blade:以下是我的刀片的代码:

@extends('layouts.adminMain')

@section('container')
    <div>
        <h3>Insert Product</h3>
        <form action="{{ url('/products') }}" enctype="multipart/form-data" method="POST">
            {{-- csrf untuk authorization --}}
            @csrf
            <label for="categories">Category</label>
            <div class="form-group">
                <select name="categories" id="categories">
                    @if (!empty($categories))
                        @foreach ($categories as $category)
                        <option value="{{$category->id}}">
                            {{$category->name}}
                        </option>
                        @endforeach
                    @endif
                </select>
            </div>
            <br><br><br>
            <div class="form-group">
                <label for="productImage">Image</label>
                <input type="file" name="image" id="productImage">
            </div>
            <div class="form-group">
                <label for="floatingName">Name</label>
                <input type="text" name="name" id="productName">
            </div>
            <div class="form-group">
                <label for="productDescription">Description</label>
                <input type="text" name="description" id="productDescription">
            </div>

            <label for="productPrice">Price</label>
            <input type="number" name="price" id="productPrice">

            <input type="submit" value="Insert">
        </form>
        {{-- di projek label ini di bedain jgn disatu page --}}
        <label for="error" style="color: red">
            @if ($errors->any)
                {{ $errors->first() }}
            @endif
        </label>
    </div>
@endsection

Below is the code of my route:以下是我的路线代码:

Route::get('/insert-products', function(Request $request){

    return view('insertProducts', [
        "title" => "Insert Product",
        "categories"=>Categories::all()
    ]);
});

Route::post('/products', [ProductsController::class, 'insertProducts'])->name('products');

It keeps giving me它不断地给我

Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException The POST method is not supported for this route. Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException 此路由不支持 POST 方法。 Supported methods: GET, HEAD.支持的方法:GET、HEAD。

You can use the route name instead您可以改用路由名称

<form action="{{ route('products') }}"></form>

Try php artisan route:cache尝试php artisan route:cache

or php artisan optimize:clearphp artisan optimize:clear

Maybe it will resolve your problem.也许它会解决你的问题。

Your post route is wrong, fix this as below:您的发布路线错误,请按以下方式解决此问题:

Route::post('/products', [ProductsController::class, 'insertProduct'])->name('products');

then run 'php artisan optimize' command.然后运行“php artisan optimize”命令。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM