I am trying to build a form to edit the information of offices. This form is build using information coming from two models: Office and Address. Like you see in the image fields marked with 1 belongs to address model, the ones marked with 2 belongs to Offices
An Office has one Address
office.php
public function address()
{
return $this->hasOne('App\Address');
}
An address belongs to an Office
address.php
public function office()
{
return $this->belongsTo('App\Office');
}
Now, in order to populate the edit view I need to bind two models to a form!
I have tried this:
{!! Form::model($element, ['method'=>'PATCH', 'action'=> ['AdminofficeController@update', $element->slug ],'files'=>true]) !!}
{!!Form::label('image', 'Upload a Featured Image') !!}
{!!Form::file('image', null, array('class' => 'form-control'))!!}
...here fields from office model...
{!! Form::label('street', 'Street:', array('class' => 'pt-3'))!!}
{!! Form::text($element->address->street, null, array('class'=>'form-control' )) !!}
... here other fields ...
{!!Form::submit('Edit Büro', array('class' => 'btn btn-success btn-block')) !!}
{!!Form::close() !!}
But I get the following error:
Property [street] does not exist on this collection instance. (View: C:\laragon\www\domainesources\views\dashboard\bueros\edit.blade.php)
This line does not solve the problem
{!! Form::text($element->address->street, null, array('class'=>'form-control' )) !!}
I have var_dump te collection and indeed, it has an street property, it means the relation is working ok.
As far as I know I can't bind two models to one form (not in HTML)
How do I populate a form with properties coming from multiple models?
I would hate to make two forms one to edit Address and another for the name of the office.