skip to Main Content

i got error 404 while applying route model binding , i am confused where i went wrong, even though i checked route and controller are correct

this is my index coding

```
<h1 class="heading"> product <span>categories</span> </h1>

<div class="box-container">
  @foreach($kategori as $k)
    <div class="box">
        <img src="image/cat-1.png" alt="">
        <h3>{{$k->nama_kategori}}</h3>
        <p>{{$k->deskripsi_kategori}}</p>
        <a href="/{{$k->slug_kategori}}" class="btn">Lihat</a>
    </div>
    @endforeach
    

</div>

“`

my routes code

<?php

use AppHttpControllersHomepageController;
use IlluminateSupportFacadesRoute;


/*
|--------------------------------------------------------------------------
| 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!
|
*/

Route::get('/', [HomepageController::class, 'index']);
Route::get('/{buku:slug}', [HomepageController::class, 'show']);
Route::get('/{kategori:slug_kategori}', [HomepageController::class, 'lihat']);

my controller code

<?php


namespace AppHttpControllers;

use IlluminateHttpRequest;
use AppModelsBuku;
use AppModelsKategori;

class HomepageController extends Controller
{
    public function index() {
      return view('homepage/index',[
      "title" => "Homepage",
      "books" => Buku::all(),
      "kategori" => Kategori::all(),
      ]);
    }

    public function show(Buku $buku) {
      return view('homepage/lihat', [
      'title' => 'Detail Buku',
      'book'  => $buku
    ]);
    }

    public function lihat(Kategori $kategori) {
      return view('homepage/kategori', [
        'title' => $kategori->nama,
        'kategoris'  => $kategori,
      ]);
    }
}

kategori migration

<?php

use IlluminateDatabaseMigrationsMigration;
use IlluminateDatabaseSchemaBlueprint;
use IlluminateSupportFacadesSchema;

return new class extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('kategoris', function (Blueprint $table) {
            $table->id();
            $table->string('kode_kategori')->unique();
            $table->string('nama_kategori')->unique();
            $table->string('slug_kategori');
            $table->text('deskripsi_kategori');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('kategoris');
    }
};

and file views structure
file views structure

i got error 404 while applying route model binding , i am confused where i went wrong, even though i checked route and controller are correct

2

Answers


  1. Try this artisan command

    php artisan config:clear

    php artisan route:clear

    Login or Signup to reply.
  2. What’s the route that shows a 404 page?, maybe it’s because of the cache, try these commands:

    • for clear route cache: $ php artisan route:clear
    • for clear all cache app: $ php artisan optimize:clear
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search