Skip to main content

Overview

Ziggy supports Laravel’s route-model binding, including custom route key names. When you pass a JavaScript object as a route parameter, Ziggy automatically extracts the correct property value using the model’s binding configuration.

How It Works

When you pass an object as a route parameter, Ziggy:
  1. Checks if the route has a custom binding key defined
  2. If yes, uses that key to extract the value from the object
  3. If no custom key exists, falls back to the id property

Custom Route Keys

Laravel models can customize their route key using getRouteKeyName():
Ziggy automatically detects this configuration and uses the slug property:
Ziggy reads the binding configuration from your Laravel routes when generating its JavaScript config. You don’t need to manually configure bindings in JavaScript.

Inline Custom Keys

Laravel allows you to specify custom binding keys directly in route definitions using the : syntax:
Ziggy respects these inline bindings:

Scoped Bindings

Ziggy supports Laravel’s scoped bindings with custom keys:
While Ziggy uses the correct binding keys to generate URLs, the actual scope validation (ensuring the comment belongs to the post) happens on the Laravel backend.

How Bindings Are Resolved

Ziggy’s PHP code analyzes your routes to determine binding keys:
This method:
  1. Inspects route parameters that implement UrlRoutable
  2. Checks if the model customizes its route key
  3. Stores the binding configuration in Ziggy’s config
  4. Merges inline binding fields from the route definition

Binding Substitution in JavaScript

When you pass an object, Ziggy’s JavaScript code extracts the value:

Error Handling

If you pass an object that doesn’t have the required binding key, Ziggy throws an error:

Fallback to ID

If no custom binding key is defined, Ziggy defaults to using the id property:

Mixed Parameter Types

You can mix objects with route-model binding and primitive values:

Best Practices

If you change a model’s getRouteKeyName() method, regenerate Ziggy’s config:
Or refresh the page if using the @routes Blade directive.

Common Use Cases

API Responses

When working with API data that includes model objects:

Vue Components

React Components

Next Steps

Default Parameters

Learn about default parameter values

TypeScript Support

Get autocomplete for routes and parameters