My controller
for($a=0; $a<count($area_id); $a++)
{
for($i=0; $i<count($estimated_time); $i++)
{
$project_area = new Projectarea;
$project_area->area_id = $area_id[$a];
$project_area->project_id = $project->id;
$project_area->estimated_time = $estimated_time[$i];
$project_area->save();
}
}
return Redirect::to('admin');
my view
<div class="form-group">
<label for="select_client">Choose area</label>
<select class="form-control" name="area_id[]" multiple="multiple">
@foreach($area as $row)
<option class="click" value="{{ $row->id }}" area="{{ $row->id }}">{{$row->area_name}}</option>
@endforeach
</select>
@foreach($area as $row)
<input class="other" id="{{ $row->id }}" type="text" name="estimated_time[]" placeholder="{{ $row->area_name }}" />
@endforeach
</div>
mysql result.
if i select for example three values from multiple select i have this result in database
project_id area_id estimated_time
123 7 0
123 7 2
123 7 3
123 7 4
123 7 0
123 8 0
123 8 2
123 8 3
123 8 4
123 8 0
123 9 0
123 9 2
123 9 3
123 9 4
123 9 0
124 8 0
Thank you
Your error is due to double foreach, you foreach too many time.
3 value selected = 3 loop (for($a=0; $a<count($area_id); $a++)
) * n loop for every estimated_time (for($i=0; $i<count($estimated_time); $i++)
)
So remove second foreach and acces POST via array :
var_dump($_POST);
for($a=0; $a<count($_POST['area_id']); $a++)
{
var_dump($_POST['estimated_time'][$_POST['area_id'][$a]]);
var_dump($_POST['area_id'][$a]);
echo 'insert';
}
Edit : it s a correction our your code, but your concption still wrong
Its difficult to understand from your question, however, if I understand it right you are trying to insert estimated time for project based on location. The problem was that you actually don't need the inner for loop, since, estimated_time array and area_id array would actually be of same size.
Please try following code:
for($i=0; $i<count($area_id); $i++)
{
$project_area = new Projectarea;
$project_area->area_id = $area_id[$i];
$project_area->project_id = $project->id;
$project_area->estimated_time = $estimated_time[$i];
$project_area->save();
}
return Redirect::to('admin');