У меня есть категории с ними дети: вот таблица и объяснение: -
Table `categories` ( id - category )
Table `children ` ( id - category_id - child )
categories
имеют отношение в модели:
public function children()
{
return $this->hasMany('App\Models\Child');
}
children
имеют отношение в Модель:
public function parent()
{
return $this->belongsTo('App\Models\Category');
}
Вот моя форма, созданная с помощью js, когда я добавляю новое поле и добавляю новое поле:
@forelse ($category->children as $child)
<div class="form-group">
<div class="input-group">
<input type="text" value="{{ $child->child }}" required name="child[]" class="form-control">
<span class="input-group-btn">
<a class="btn bg-danger bg-danger2 btn_remove">
<i class="sicon-cancel"></i>
</a>
</span>
</div>
</div>
@empty
<p class="font-13">there is no child for this category</p>
@endforelse
<button type="button" id="add_button" class="btn btn-tiffany">add new field </button>
Дизайн:
Мой закрытый контроллер:
public function update(Request $request, Category $category)
{
$category->update($request->only('category'));
if($request->children){
$category->children()->updateOrCreate($data['child']);
}
}
Как справиться с if (удалить одного из дочерних или обновить его или добавить новый)?