Back

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() ?

1

191

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.

1

19

How does your livewire component look like

1

20

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');
}

}

2

88

If I try to `dd()` the $post in Livewire component, it throws error to not use it without initialization.

1

15

Does it maybe need to be DI'd in the `render` function? Like it would in a controller?

1

53

In response to @NikSpyratos

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');
}

}

48

  • No matching results...
  • Searching...

/ 1000