Search code examples
laraveleloquentlaravel-bladelaravel-routinglaravel-8

Laravel route model binding for Blade view component


In my service provider, I bind the event model in the route.

Route::model('event', Event::class);

Then I create the following route.

Route::view('/events/{event}/overview', 'cp.event-overview')

In this view, I call a blade component that looks like this.

class EventHeader extends Component
{
    public $event;

    public function __construct(Event $event)
    {
        $this->event = $event;
        dd($event);
    }
}

The code returns an empty model (exist: false). But if I do the same and forward the route to a controller, then is it working. Are there any ways to inject the model into Blade components?


Solution

  • Assuming you are calling the component in your blade view, you can pass the Event like that:

    <event-header :event="request()->route('event')"></event-header>