Laravel Tutorials

Laravel Scoped Route Binding for Nested Resource Management

Published
Laravel Scoped Route Binding for Nested Resource Management image

Laravel's scoped implicit model binding transforms how developers handle nested resources by automatically enforcing parent-child relationships. This feature eliminates manual validation while creating cleaner, more secure URL structures for hierarchical data.

The scoped method enables automatic relationship validation when defining nested routes:

use App\Http\Controllers\OrderItemController;

Route::resource('orders.items', OrderItemController::class)->scoped([
    'item' => 'sku',
]);

This configuration generates URLs following the pattern /orders/{order}/items/{item:sku} where Laravel automatically validates that each item belongs to its specified order.

Laravel leverages naming conventions to determine relationships between models. The framework assumes the parent model contains a relationship method matching the pluralized route parameter name. When a request targets /orders/123/items/ABC789, Laravel queries the items relationship on the Order model to locate the item with SKU "ABC789".

Building an e-commerce inventory system demonstrates this functionality effectively:

Route::resource('warehouses.products', WarehouseProductController::class)->scoped([
    'product' => 'barcode',
]);

class Warehouse extends Model
{
    public function products()
    {
        return $this->hasMany(Product::class);
    }
}

class Product extends Model
{
    public function warehouse()
    {
        return $this->belongsTo(Warehouse::class);
    }
}

class WarehouseProductController extends Controller
{
    public function edit(Warehouse $warehouse, Product $product)
    {
        return view('inventory.edit', compact('warehouse', 'product'));
    }
    
    public function update(Warehouse $warehouse, Product $product, Request $request)
    {
        $product->update($request->validated());
        return redirect()->route('warehouses.products.show', [$warehouse, $product]);
    }
}

The framework automatically returns 404 responses when attempting to access products that don't exist in the specified warehouse. This URL structure /warehouses/east-coast/products/BAR123456 ensures data integrity without additional controller logic while maintaining clean, descriptive routes.

Harris Raftopoulos photo

Senior Software Engineer • Staff & Educator @ Laravel News • Co-organizer @ Laravel Greece Meetup

Sponsored

serpapi logo
SerpApi

The Web Search API for Your LLM and AI Applications

Visit SerpApi

The latest

View all →
Load Eloquent Models From Form Request Input in Laravel image

Load Eloquent Models From Form Request Input in Laravel

Read article
Laravel FFmpeg 9: Encode Video and Stream HLS and DASH image

Laravel FFmpeg 9: Encode Video and Stream HLS and DASH

Read article
Migration Durations and Job Memory Usage in Laravel 13.45 image

Migration Durations and Job Memory Usage in Laravel 13.45

Read article
Laravel CLI: One Binary for New PHP and Rust Laravel Apps image

Laravel CLI: One Binary for New PHP and Rust Laravel Apps

Read article
Phactorial: Render LaTeX Math to SVG and PNG in PHP image

Phactorial: Render LaTeX Math to SVG and PNG in PHP

Read article
Laravel, reimagined image

Laravel, reimagined

Read article