I can't use InlineCreate Operation for the case of HasMany relationship in laravel backpack 5.
I've a page for creating new item group which will allow to add multiple attributes and multiple attribute values for each corresponding attribute.
ItemGroup model has following function
public function attributes(): HasMany
{
return $this->hasMany(Attribute::class);
}
Attribute model has following function
public function attributeValues(): HasMany
{
return $this->hasMany(AttributeValue::class);
}
Item group creation page: https://i.postimg.cc/zfdV7JhB/Screen-Shot-2023-03-06-at-1-47-20-PM.png
In ItemGroupCrudController, I tried to add the following code block to achieve inline creation of Attribute values. But this did nothing.
use InlineCreateOperation;
...
...
protected function setupCreateOperation(): void
{
...
$this->crud->addField([
'name' => 'attributes',
'type' => "relationship",
'subfields' => [
[
'name' => 'name',
'label' => "Attribute",
'wrapper' => ['class' => 'form-group col-md-6']
],
[
'name' => 'attributeValues',
'label' => "Values",
'wrapper' => ['class' => 'form-group col-md-6']
'type' => "relationship",
'ajax' => true,
'inline_create' => true,
]
],
]);
The reason I guess inline create operation does not work for HasMany relation.
Please suggest a solution.
Hmm... difficult to know when there is no error message. But two things stand out to me:
(1)
'inline_create' => trueonly works for single relationships (1-1, 1-n). For multiple relationships (n-n) you have to define the entity. Take a look at Step 2 in the docs of InlineCreate - https://backpackforlaravel.com/docs/5.x/crud-operation-inline-create#how-to-use. In your case I believe it would be:(2) Please make sure there is a CRUD for
attributeValues. That is what InlineCreate does, it takes the Create screen from that CRUD... and uses it inside another CRUD.