I want to update the model Chapters (table: Chapters).
So far it removes all data to that model and only inserts the last data I put in my form.
Here is a little scretch of the 2 database tables:
Chapters:
id | product_id | name
_________________________________________________
3 | 2 | jon doe
Products:
id | ...
_____________________________________
2 | ...
I want to update / add a chapter to my chapters table.
My update method:
public function updateProduct(Request $request)
{
...
$productID = $data['id'];
$product = Product::find($productID);
...
$chapters = $request->input('chapters', []);
if(count($chapters) > 0)
{
$chaptersData = array();
$o = 1;
$inputChapters = Chapters::where('product_id', $product->id)->first();
foreach ($chapters as $chapterKey => $chapterValue)
{
$inputChapters->chapter_name = $chapterValue['chapter_name'];
# .. some more
$chaptersData[] = $inputChapters;
$inputChapters->product()->associate($product);
$inputChapters->save();
$o++;
}
}
...
}
via utdev