I have a product table which has price, name, stock etc.
and color_products table which has color_id, product_id
. I want to store a product to the database with selected colors so far I'm getting an error Invalid argument supplied for foreach()
. How can I fix this?
Controller
$formInput=$request->all(); //If I do dd($formInput) I see all data
$product = product::create(array_merge($formInput, [
'user_id' => Auth::user()->id
]));
// Insert color to the database
$store_color = [];
$colors = $request->color_id;
foreach ($colors as $color) {
$ouzColor = $color->save;
$store_color[] = [
'product_id' => $product->id,
'color_id' => $ouzColor
];
}
ColorProduct::insert($store_color);
Blade file
<select multiple name="color_id" id="color">
<option value=""> --Select Colors -- </option>
@foreach($colors as $color)
<option value="" ></option>
@endforeach
</select>
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire