@markfinney Do you want to show everyone your code so we can help?
Struggling with Forms - two models, one form, edit mode
I have only used forms with model binding so far in laravel, and am really struggling make a form work in edit mode.
I have a parent child one to many relationship set up. But in a certain specific case I need a form that will edit the parent and its 'single' child (I know I should have created a sibling model and would have if the system spec had been even remotely accurate!). However, I know have a few hours in which to work magic and am tearing my hair out.
Just to be clear in most cases the parent has many children, but one scenario results in the parent having one child - this is the scenario I need to edit this parent and child in a single form.
Can I use form::open() and pipe the return values in? I can't seem to findanything about this?
Thank you in advance if you can give me so advice...
If it's a 1-1 relation, then simply bind the parent:
// Imagine these objects:
// $parent->foo
// $child->bar
// you need to eager load the child in order to populate the form
$parent = Parent::with('child')->find($id);
Form::model($parent, ...)
Form::text('foo', ...)
Form::text('child[bar]', ...)
then for processing something like this:
$parent = Parent::find($id);
$parent->fill(Input::except('child'));
$child = $parent->child;
$child->fill(Input::get('child'));
If 1-1 is not the case, then simply use accessor:
// Parent model
public function children() {...} // your hasMany relation
public function getChildAttribute()
{
return $this->children()->first(); // or find($someId) or however you need to get this child
// or return $this->children->find($someId); // whatever suits you
}
rest of the code remains unchanged.
Please or to participate in this conversation.