0

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 App\Http\Controllers\HomepageController;
use Illuminate\Support\Facades\Route;


/*
|--------------------------------------------------------------------------
| 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 App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Buku;
use App\Models\Kategori;

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 Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

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

  • your **show** and **lihat** both points to same type of segments and hence only **show** will be kicked in (since it is above **lihat** in routes file). – itachi Feb 13 '23 at 17:17
  • You have 2 wildcard routes. The first one will match everything, every time. `/foo` -> `{buku:slug}` matches. `/bar` -> `{buku:slug}` matches. `/anything` -> `{buku:slug}` matches. You need to to be able to tell the 2 routes apart, independent of the wildcard, eg maybe `/buku/{buku:slug}` and `/kategori/{kategori:slug_kategori}`. Or if your slugs follow some pattern maybe you can [constrain them](https://laravel.com/docs/9.x/routing#parameters-regular-expression-constraints). – Don't Panic Feb 13 '23 at 23:13

2 Answers2

0

Try this artisan command

php artisan config:clear

php artisan route:clear

0

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