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:- Checks if the route has a custom binding key defined
- If yes, uses that key to extract the value from the object
- If no custom key exists, falls back to the
idproperty
Custom Route Keys
Laravel models can customize their route key usinggetRouteKeyName():
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:
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:Binding Resolution Process (Ziggy.php)
Binding Resolution Process (Ziggy.php)
- Inspects route parameters that implement
UrlRoutable - Checks if the model customizes its route key
- Stores the binding configuration in Ziggy’s config
- Merges inline binding fields from the route definition
Binding Substitution in JavaScript
When you pass an object, Ziggy’s JavaScript code extracts the value:Binding Substitution (Router.js)
Binding Substitution (Router.js)
Error Handling
Fallback to ID
If no custom binding key is defined, Ziggy defaults to using theid property:
Mixed Parameter Types
You can mix objects with route-model binding and primitive values:Best Practices
Pass full model objects when available
Pass full model objects when available
Ensure objects have the required keys
Ensure objects have the required keys
Re-generate Ziggy config after model changes
Re-generate Ziggy config after model changes
If you change a model’s Or refresh the page if using the
getRouteKeyName() method, regenerate Ziggy’s config:@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