skip to Main Content

My error

The GET method is not supported for route produk/index. Supported methods: POST.

   Route::post('/produk/index', [ProdukController::class,'index'])->name('produk.index');

web.php

<?php

use IlluminateSupportFacadesRoute;
use AppHttpControllersLoginController;
use AppHttpControllersProdukController;
use AppHttpControllersSupplierController;


/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider and all of them will
| be assigned to the "web" middleware group. Make something great!
|
*/

Route::get('/home', function () {
    return view('partials/main');})->middleware('auth');


Route::get('/login', [LoginController::class, 'index'])->name('login')->middleware('guest');
Route::post('/login', [LoginController::class, 'authenticate']);
Route::get('/logout', [LoginController::class, 'logout']);

    Route::get('/produk/data', [ProdukController::class, 'show'])->name('produk.data');
    Route::post('/produk/index', [ProdukController::class,'index'])->name('produk.index');
    Route::get('/supplier/data', [SupplierController::class, 'index'])->name('supplier.data');

Controller ProdukController.php

<?php

namespace AppHttpControllers;

use AppModelsProduk;
use IlluminateHttpRequest;
use IlluminateRoutingController;

class ProdukController extends Controller
{
    public function show() {

    return view('kategori.kategori');
    }
    public function index(Request $request)
    {
        // dd($request);
        $ValidateData = $request->validate([
        'nama_produk' => ['required','max:255'],
        'harga_beli' => ['required'],
        'harga_jual' => ['required'],
        'stok' => ['required']

    ]);
    Produk::create($ValidateData);

        return view('kategori.index')->with('succes');
    }
};

Views index.blade.php

<!doctype html>
<html lang="en">
  <head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <!-- Bootstrap CSS -->
    <link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/font/bootstrap-icons.min.css">
    <link rel="stylesheet" href="{{ asset('css/bootstrap.min.css') }}">
    <link rel="stylesheet" href="{{ asset('css/style.css') }}">

    <title>Form Produk</title>
  </head>
  <body>
    {{-- @foreach ($produks as $produk ) --}}

    {{-- @endforeach --}}
      <div class="col-6 mx-auto">
        <div class="container bg-white">
                     <form action="/produk/index" method="POST">
                @csrf
        <div class="mb-3">
            <label for="exampleFormControlInput1" class="form-label">Nama Produk</label>
            <input class="form-control" name="nama_produk" id="exampleFormControlInput1" placeholder="nama produk">
          </div>
        <div class="mb-3">
            <label for="exampleFormControlInput1" class="form-label">Harga Beli</label>
            <input class="form-control" name="harga_beli" id="exampleFormControlInput1" placeholder="harga beli">
          </div>
        <div class="mb-3">
            <label for="exampleFormControlInput1" class="form-label">Harga Jual</label>
            <input class="form-control" name="harga_jual" id="exampleFormControlInput1" placeholder="harga jual">
          </div>

        <div class="mb-2">
            <label for="exampleFormControlInput1" class="form-label">Stok</label>
            <input class="form-control" name="stok" id="exampleFormControlInput1" placeholder="stok">
          </div>
          <button onclick="" class="btn btn-success mt-3 mx-auto mb-3 text-center" type="submit"><i class="bi bi-plus-lg"></i> Tambah</button>
        </form>
        </div>
        </div>


    <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM" crossorigin="anonymous"></script>
    <script src="https://cdn.jsdelivr.net/npm/@popperjs/[email protected]/dist/umd/popper.min.js" integrity="sha384-IQsoLXl5PILFhosVNubq5LC7Qb9DXgDA9i+tQ8Zj3iwWAwPtgFTxbJ8NT4GN1R8p" crossorigin="anonymous"></script>
    <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.min.js" integrity="sha384-cVKIPhGWiC2Al4u+LWgxfKTRIcfu0JTxR+EQDz/bgldoEyl4H0zUF0QKbrJ0EcQF" crossorigin="anonymous"></script>

  </body>
</html>
  </body>
</html>

help me in resolving this error

2

Answers


  1. This is because the route for your index is written as POST, use GET instead.

       Route::get('/produk/index', [ProdukController::class,'index'])->name('produk.index');
    

    For you to store your products, I suggest to create a store method. Meaning the code would look like the following:

       Route::get('/produk/index', [ProdukController::class,'index'])->name('produk.index');
       Route::post('/produk/store', [ProdukController::class,'store'])->name('produk.store');
    
        public function index(Request $request)
        {
            return view('kategori.index'); // or where your index/create blade file.
        }
    
        public function store(Request $request)
        {
            // dd($request);
            $ValidateData = $request->validate([
                'nama_produk' => ['required','max:255'],
                'harga_beli' => ['required'],
                'harga_jual' => ['required'],
                'stok' => ['required']
            ]);
            Produk::create($ValidateData);
    
            return redirect()->route('kategori.index')->with('succes');
        }
    

    Then in your index.blade.php, change

    <form action="/produk/index" method="POST">
    

    to

    <form action="{{route(produk.store)}}" method="POST">
    
    Login or Signup to reply.
  2. Please follow below 3 steps and then check if it resolved your issue

    1. Below change should be done in route file as below:

      Route::get(‘/produk/index’, [ProdukController::class,’index’])->name(‘produk.index’);

    2. Move create code form index function to new function as below:

      public createProduct(){
      Produk::create($ValidateData);
      return view(‘kategori.index’)->with(‘succes’);
      }
      };

    3. Then defined new route in route file for post method

    4. Change form action in your html with this newly added route

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search