All good points.
I really want this API to feel simple, clean and intuitive. Also, I want to offer a flexible alternative for more control.
Here’s the API currently rattling around in my head:
class ShowPost extends Component
{
public $post;
protected $casts = ['post' => 'model'];
public function mount(Post $post)
{
$this->post = $post;
}
}
Internally Livewire will “dehydrate” the model to it’s class, and id. (I still have questions about what this will look like in the payload).
It WONT automatically re-hydrate it, until $this->post
is called in the component (I’ll work some PHP magic to make this happen). At which time it will use “Post::find($id)” to hydrate it.
If the user wants anything more (hiding the full model class, adding eager loads), we can do one of two things:
#1: Allow custom “casters” (PHP classes that offer a hydrate
and dehydrate
method). For example:
protected $casts = ['post' => PostCaster::class];
...
class PostCaster implements LivewireCaster
{
public function hydrate($value) {}
public function dehydrate($value) {}
}
#2: Just use the “computed property” api I proposed earlier. (doesn’t exist yet)
class ShowPost extends Component
{
pubic $postId;
public function mount(Post $post)
{
$this->postId = $post->id;
}
public function getPostAttribute()
{
return Post::find($this->postId);
}
}
So, I suppose my question is:
A) Is the proposed default strategy (store the class and id, and rehydrate using ::find
) a common enough path to cover the average use case? (I’m fine with just warning them about what’s happening in the docs (exposing the class namespace))