I'm trying to create multi selection bootstrap output to save in database but I'm getting an array in my controller like ["6","7"]
. How I can save this?
There is 2 selection that means 2 row must created.
The query I was trying:
$table = Pool_user::updateOrInsert(['pool_id' => [$string1], 'user_id' => $request['id']]);
Can anyone help me to solve this?
By responding directly to your question, you need to create an array of arrays with the values you want to enter.
Something like this:
$array = [];
foreach($request['pool_ids'] as $pool_id){ //$request['pool_ids'] -> ["6","7"]
array_push($array, [
'pool_id' => $pool_id,
'user_id' => $request['id']
]);
}
$table = Pool_user::insert($array);
However, considering that Pool_user
is a Pivot table of a Many to Many relationship, there is a much better method of doing this. Let's say in User Model, have a relationship called polls()
.
You can simply ask the Eloquent to synchronize the relationships:
$user = User::find($request['id']);
$user->polls()->sync($request['pool_ids']); //$request['pool_ids'] -> ["6","7"]
Hope this helps