admin
admin copied to clipboard
Custom form item or view form another model?
In my application in the edit (or create) form of modelA I wuold like to have the possibility to create an object (new record) fo another model, modelB. For example, modelA have an imageid that is a reference to modelB. Inside the form of model A I can show the image, using the relationship and Column::image('modelB.image_url') inside the form.
I would like to have the possibility to upload an image from this form (_modelA form_) and save it:
- save ModelB image
- connect it to modelA. It is something similar to multiselect save function in many to many relationship
public function setCategoriesAttribute($categories)
{...}
The problem is that I can't create, inside a custom method in modelA, a new record of modelB, is it possibile?
The alternative is to use a tabbed form (as in contacts3 of v3 demo) where the _Display from Related Model (Companies)_ is used to add an item to a related Model.
Thanks, Carlo
Me too! I would love some input on this. Thanks
I have the necessity to create from a form both a new istance of the connected model and a new istance of a related model.
I am using this solution (example from admin3 demo: Form - Model contact
->create(function ()
{
FormItem::text('firstName', 'First Name')->required(),
FormItem::text('lastName', 'Last Name')->required(),
FormItem::text('companyTitle', 'Company')->required(),
FormItem::text('companyAddress', 'Company Address')->required(),
Model
public function setCompanyTitleAttribute($item)
{
$this->save(); //save Contact so the id (model key) is created
$company = new Company(array('title' => $item));
$company->save();
$this->companies()->attach($company);
}
public function setCompanyAddressAttribute($item)
{
}
}
This solution works to create a new Company inside the Contact model and connect it to the contact. Use something like this is correct? However, the problem is when the related model (company in this case) has more than one attribute (Title and Address). I should create the model only when the first attribute is processed, then update the company istance when the second *public setXXXAttribute($item) * is called.
In which order attributes from a form are elaborated? It seems the same order they have inside the form..is it right?