lundi 26 septembre 2016

store array values with multiple select box using laravel

I am trying to store array values by using for loop in laravel post function but the select box values are getting stored all together.

My View:

<table>
<tr>
<td>
 <input type="text" class="form-control" autocomplete="off" name="particulars[]" >
</td>
<td>
<select multiple name="event[]">
<option value="">--select--</option>
<option value="E040">Event1</option>
<option value="E034">Event2</option>
<option value="E032">Event3</option>
<option value="E035">Event4</option>
</select>
</td>
</tr>

</table> 

my controller:

for($i = 0; $i < count($data['particulars']); $i++) {

        $c= new dailystatus(); // table name
        $c->particulars = $data['particulars'][$i];

        if(!empty($data['event'][$i])){

                foreach($data['event'] as $eventname){

                    $repnamecode=$eventname;
                    $result=(explode('|', $repnamecode, 2));
                    $ecode[]=trim($result[0]);
                    $ename[]=trim($result[1]);

                    $evcode=implode($ecode, ',');
                    $evname=implode($ename, ',');

                }else{

                    $evcode=0;
                    $evname=0;

                }

        $c->event = $evcode;
        $c->evname=$evname;
        $c->save();

        }

}

the values in database suppose to be stored like below in the image:

enter image description here

But the values are getting doubled and storing like below:

enter image description here

So can you please suggest me how to avoid this duplication ? any help would be appreciated.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire