Home > Mobile >  Problem in inserting data to database using POST in laravel 8
Problem in inserting data to database using POST in laravel 8

Time:01-06

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. 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:

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 >
                <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 >
                <label for="productImage">Image</label>
                <input type="file" name="image" id="productImage">
            </div>
            <div >
                <label for="floatingName">Name</label>
                <input type="text" name="name" id="productName">
            </div>
            <div >
                <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. Supported methods: GET, HEAD.

CodePudding user response:

You can use the route name instead

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

CodePudding user response:

Try php artisan route:cache

or php artisan optimize:clear

Maybe it will resolve your problem.

  •  Tags:  
  • Related