简体   繁体   中英

Insert arrays into database

产量

So this is the output I get after submitting my form. Now I need to insert them into the database. The "sun","mon".. key are static and so is the "start",'end' key too.

So I have done the following for all the days. I know that it is not efficient. Is there a better way?

for($i=0;$i<2;$i++){
    //dump(['start'=> $data['sun'][$i]['start'],'end'=> $data['sun'][$i]['end']);
    $this->routineModel->create([
        'start'=> $data['sun'][$i]['start'],
        'end'=> $data['sun'][$i]['end'],
        'group_id' => $data['group_id'],
        'day_id' => 1,
        'schedule_id' => $data['schedule_id']
    ]);
}

This is my database: 数据库

This is my HTML form:

<label>Sunday:</label>
<div class="form-group">
    <label>From:</label>
    <input class="form-control" name="sun[0][start]" type="date">
    <label>To:</label>
    <input class="form-control" name="sun[0][end]" type="date">
</div>

<div class="form-group">
    <label>From:</label>
    <input class="form-control" name="sun[1][start]" type="date">
    <label>To:</label>
    <input class="form-control" name="sun[1][end]" type="date">
</div>

And I am using the lumen framework.

您可以使用序列化来保存整个数组

$serialized_array=serialize($your_array);

And directly save it in the database.

While fetching the rows use unserialize to get array back

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM