Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

hjortur17's avatar

Slug not working with Resources

So for some reason when I pass along some data I get 404, but if I send in an empty array the site works (not getting 404). Has anyone idea what can be causing this to happen?

I am collection classes and wrapping them into a Resource and it works like that for other sites in this project but not for this one. Any idea why that is happing?

public function show(CarClass $carClass)
{
        $resp = new CarResource(CarClass::where('slug', $carClass->slug)->firstOrFail());

        return Inertia::render('Cars/Show', ['car' => $resp]);
}
Route::get('/our-cars/{carClass:slug}', [CarController::class, 'show']);
ADDED THIS TO CARCLASS MODEL
public function getRouteKey()
{
        return $this->slug;
}
0 likes
21 replies
Sinnbeck's avatar

Why are you getting the CarClass model twice ?

public function show(CarClass $carClass)
    {
        $resp = new CarResource($carClass);

        return Inertia::render('Cars/Show', ['car' => $resp]);
    }

Remove this

public function getRouteKey() //only needed if you want it to always use slug
{
        return $this->slug;
}

or fix it

public function getRouteKey()
{
        return 'slug';
}
Sinnbeck's avatar

@hjortur17 Start by removing getRouteKey() for now as you are specifying the key in the route already. Still same error ?

Sinnbeck's avatar

@hjortur17 Then it seems that no CarClass exists with the slug. What is the url you are visiting ?

hjortur17's avatar

@Sinnbeck - I am visiting: /our-cars/tesla-model-y

And I dump the resp I get the correct car

hjortur17's avatar

@Sinnbeck - So like this:

$resp = new CarResource($carClass);
dd($resp);
App\Http\Resources\CarResource {#1023 ▼ // app/Http/Controllers/CarController.php:34
  +resource: App\Models\CarClass {#621 ▼
    #connection: "mysql"
    #table: "car_classes"
    #primaryKey: "id"
    #keyType: "int"
    +incrementing: true
    #with: []
    #withCount: []
    +preventsLazyLoading: false
    #perPage: 15
    +exists: true
    +wasRecentlyCreated: false
    #escapeWhenCastingToString: false
    #attributes: array:12 [▶]
    #original: array:12 [▼
      "id" => 1
      "slug" => "tesla-model-y"
      "title" => "Tesla Model Y"
      "description" => "The Tesla family car. In every way the same as the Model 3 just BIGGER. Offers you and your family the space that you need while on the road whilst also being a ▶"
      "drive" => "AWD"
      "seating" => 5
      "luggage" => 5
      "image_folder_url" => "https://res.cloudinary.com/..."
      "price_per_day" => "23000"
      "available" => 0
      "created_at" => null
      "updated_at" => null
    ]
    #changes: []
    #casts: []
    #classCastCache: []
    #attributeCastCache: []
    #dates: []
    #dateFormat: null
    #appends: []
    #dispatchesEvents: []
    #observables: []
    #relations: []
    #touches: []
    +timestamps: true
    #hidden: []
    #visible: []
    #fillable: []
    #guarded: array:1 [▶]
  }
Sinnbeck's avatar

@hjortur17 So it now works or ? Where did you put that code ? It looks like it is inside that method you were showing originally ?

hjortur17's avatar

@Sinnbeck - No, still getting same error (404). This is in the controller responsible for the route. So like I mention above I was always able to collect the data but if I pass it along to this page I get 404, but if I do ['car' => []] then it work.

This is the controller method show:

public function show(CarClass $carClass)
{
        $resp = new CarResource($carClass);

        dd($resp);

		// THIS FAILS: ['car' => $resp]
		// THIS WORKS: ['car' => []]

        return Inertia::render('Cars/Show', ['car' => $resp]);
}
Sinnbeck's avatar

@hjortur17 Sorry I am confused. You are saying you get 404 (which mean the url does not exist), yet also say that you get to the controller method ?

hjortur17's avatar

@Sinnbeck - So yeah, this is quite weird. So I will try to explain this better:

If I am passing along to the view this $resp data I get 404. Like the url does NOT exist. But if I replace the $resp with an empty array then I am able to load the page (like the url does exist).

If I run the command: php artisan route:list, I see this: GET|HEAD our-cars/{carClass} .... CarController@show

And keep in mind CarClass $carClass is returning the right instance of the CarClass.

So basically what is wrong is the $resp, as soon as I insert the CarClass to the variable and pass that along to the view, I get 404. If the $resp is just an empty array, everything works.

Does this clarify?

Sinnbeck's avatar

@hjortur17 Yeah but it makes no sense why it happens. Laravel or inertia does not do a redirect after the return, so there is no way that should happen. Does the url change when you get the 404 ?

Sinnbeck's avatar

@hjortur17 Ah so it is not due to this code, but rather something you are doing inside you vue component. I assume there is an Inertia.get() or similar that triggers if the car has data.

hjortur17's avatar

@Sinnbeck - So yes, I have $inertia.visit

<Card
	v-for="carClass in carClasses"
	:item="carClass"
	@click="$inertia.visit('/our-cars/' + carClass.slug)"
></Card>
hjortur17's avatar

And this is how this carClass is in my view:

{
  "slug": "tesla-model-y",
  "title": "Tesla Model Y",
  "description": "The Tesla family car...",
  "drive": "AWD",
  "seating": 5,
  "luggage": 5,
  "price_per_day": "23000",
  "images": "https://res.cloudinary.com/..."
}
hjortur17's avatar

@sinnbeck - So I guess this has something to do with my Resource. If I skip it and just pass $carClass straight into the view, everything works. Any idea why that is happening?

hjortur17's avatar

I feel so stupid 🤦‍♂️ I was using the wrong Resource class the hole time!

Thanks for trying to solve this with me

Please or to participate in this conversation.