For some reasons, I cannot get the model in blade, even though route model binding is set in the model to 'slug' key, but in the blade I just get the string of slug, but not model. In Livewire component, I did set public Post $post. Do I need to explicitly pass it to view via render() ?
Back
resources/views/home/post.blade.php
app/Livewire/Home/Post.php
•
In response to @sharoon
But I think, it should work out of the box without needing binding the route in AppServiceProvider.
It only passes the model when I define the following in AppServiceProvider:
Route::bind('post', fn(string $post): Post =>
Post::whereSlug($post)->firstOrFail());
But I think, it should work out of the box without needing binding the route in AppServiceProvider.
•
web.php
Route::prefix('posts')->group(function () {
Route::view('/{post:slug}', 'home/post')->name('home.post');
});
resources/views/home/post.blade.php
<x-app-layout>
<div class="flex flex-col items-center justify-center">
<div class="w-full max-w-lg px-2 overflow-hidden rounded-lg dark:shadow-md sm:px-0">
@dd($post)
<livewire:home.post />
</div>
</div>
</x-app-layout>
app/Livewire/Home/Post.php
final class Post extends Component
{
public Post $post;
public function render(): View
{
return view('livewire.post');
}
}
•
If I try to `dd()` the $post in Livewire component, it throws error to not use it without initialization.
•
Does it maybe need to be DI'd in the `render` function? Like it would in a controller?
•
When I do the following in the Component, it dumps the empty instance of the Post model.
final class Post extends Component
{
//public Post $post;
public function render(Post $post): View
{
dd($post);
return view('livewire.post');
}
}
You can directly reference the component in the route. Otherwise you have to pass the post to the livewire component