Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Vinze's avatar
Level 3

Make parent component property available to child components

Would it (theoretically) be possible to build the following form components:

<x-form :model="$article">
    <x-form.input name="title" /> <!--populated with $article->title -->
    <x-form.textarea name="content" /> <!--populated with $article->content -->
</x-form>

At the moment I've solved it by passing the $article model to all form components, like this:

<x-form>
    <x-form.input name="title" :bind="$article" />
    <x-form.textarea name="content" :bind="$article"  />
</x-form>

Here I use the $bind property to get the value based on the name. Of course this works fine, but it would be really awesome if it was possible to access the parent model inside the child components without explicitly passing it as a property all the time. Something like this:

class Input extends Component {
    public function __construct($name = null) {
        $value = old($name, $this->model->{$name}); // just an example
    }
}

I've tried to accomplish this by storing the model in the session with session(['model' => $model]) on the Form component. Then in the child Input components I'm retrieving it from the session and populating the value. This works but doesn't really feel right and kinda hacky. I've also found the protonemedia/laravel-form-components library on Github which uses a @bind directive, but is that the only (right?) way? I'm guessing (after typing this whole question) the answer is yes.

Any thoughts/suggestions? :)

0 likes
0 replies

Please or to participate in this conversation.