Binding to Model relationship

Hello,

I tried to find a related topic without any luck. If there is such a topic and missed it, sorry to waste your time.

I am having some trouble finding a way to bind a model relationship in order to use it as a field in the edit form for that model.

While there is the

public Product $product;

I am looking for a way to have a field in the edit form wired to the relationship.

Is this possible?

Thank you very much,

Hey, @ClementN
You can do the following:

Let’s assume you want to edit a post;

In your route:

Route::get('/post/{post}', EditPost::class)->name('post.edit');

In your Component

....
public $title;
public $categoryName;
public function mount(Post $post)
{
  $categoryName = $post->category->name;
  $this->title = $post->title;
}

Thank you very much, @skywalker.

I was wondering if the new feature (v2) allowing Binding Directly To Model Properties
has the ability to bind with the model relationships, too.

However, your suggestion will do it.

Thank you again,

You are very welcome @ClementN

Honestly, I do Model binding this way and I’m not familiar with the new feature of binding the model directly.

In all cases I’m glade that my answer fit your need.

Happy coding mate :slight_smile: