PHP 如何使用 Laravel 5 将数据从数组保存到 mysql

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30496740/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 11:39:12  来源:igfitidea点击:

PHP How to save data from array to mysql using laravel 5

phpmysqlarrayslaravellaravel-5

提问by ray sn0w

Im getting an array within array of 'Cylinders' data from POST:

我从 POST 中获取“圆柱”数据数组中的一个数组:

Array
  (
    [serie] => Array
        (
            [0] => 1234
            [1] => 3545
        )

    [seriesap] => Array
        (
            [0] => 1234234
            [1] => 345345
        )

    [type] => Array
        (
            [0] => 4546
            [1] => csdfwe
        )

    [admission] => Array
        (
            [0] => 04-05-2015
            [1] => 04-05-2015
        )

    [invoice] => Array
        (
            [0] => fei76867
            [1] => feiasodjf
        )
  )

Now, the fields inside the keys: serie, type, admission, etc dont change, but the info inside those key do change, i mean there could be even 15 items in there.

现在,键内的字段:系列、类型、入场等不会改变,但这些键内的信息确实会改变,我的意思是那里甚至可能有 15 个项目。

At the end i need to save to the database:

最后我需要保存到数据库:

$cylinder            = new Cylinder();
$cylinder->serie     = ??;
$cylinder->seriesap  = ??;
$cylinder->type      = ??;
$cylinder->admission = ??;
$cylinder->invoice   = ??;
$cylinder->save

How can i accomplish this task and save all the cylinders?

我怎样才能完成这个任务并保存所有的气瓶?

I have tried all the foreach's that i could think of nothing seems to work.

我已经尝试了所有我能想到的 foreach 似乎没有任何效果。

/edit/

/编辑/

This is what Im doing so far:

这就是我到目前为止所做的:

$cyldata = $_POST['cylinder']; //this is the post from top.

$num_elements = 0;

while($num_elements < count($cyldata['serie'])){
    $cylinder = new Cylinder();
    $cylinder->serie     = $cyldata['serie'][$num_elements];
    $cylinder->type      = $cyldata['type'][$num_elements];
    $cylinder->admission = $cyldata['admission'][$num_elements];
    $cylinder->seriesap  = $cyldata['seriesap'][$num_elements];         
    $cylinder->save
    $num_elements++;

}

But it feels ugly, all those saves doesnt feel right. Dirty solution if you ask me.

但是感觉很丑,所有这些保存都感觉不对。如果你问我,肮脏的解决方案。

采纳答案by Hieu Le

First, you need to convert you input data to another format:

首先,您需要将输入数据转换为另一种格式:

$cyldata = $_POST['cylinder']; //this is the post from top.

$num_elements = 0;

$sqlData = array();

while($num_elements < count($cyldata['serie'])){
    $sqlData[] = array(
        'serie'         => $cyldata['serie'][$num_elements],
        'type'          => $cyldata['type'][$num_elements],
        'admission'     => $cyldata['admission'][$num_elements],
        'seriesap'      => $cyldata['seriesap'][$num_elements],
        'invoice'       => $cyldata['invoice'][$num_elements], // you miss this field, aren't you?
        'created_at'    => Carbon\Carbon::now(), // only if your table has this column
        'updated_at'    => Carbon\Carbon::now(), // only if your table has this column
    );
    $num_elements++;
}

Second, use the Fluent query builder to do a batch insert:

其次,使用 Fluent 查询构建器进行批量插入:

DB::table('table_name')->insert($sqlData);

Note:the created_atand updated_atappear here if your table has these field. When working with Eloquent model, these field is updated automatically. However, we do not use Eloquent, so that we have to assign the value to these field manually.

注意:如果您的表格有这些字段,则created_atupdated_at会出现在此处。使用 Eloquent 模型时,这些字段会自动更新。但是,我们不使用 Eloquent,因此我们必须手动为这些字段赋值。

回答by Saiyan Prince

I don't know what forced you to use $_POSTglobal array in order to receive the data from the user.

我不知道是什么迫使您使用$_POST全局数组来接收用户的数据。

Perhaps, this is what you want.

或许,这就是你想要的。

/**
 * Store the form inputs in the table
 *
 * @param Request $request
 */
public function store( Request $request ) {
    $data = Input::get();

    for($i = 0; $i < count($data['serie']); $i++) {
        $c            = new Cylinder();
        $c->serie     = $data['serie'][$i];
        $c->type      = $data['type'][$i];
        $c->admission = $data['admission'][$i];
        $c->seriesap  = $data['seriesap'][$i];

        $c->save(); // fixed typo
    }
}

回答by Bagaskara Wisnu Gunawan

Because you are having an array to insert the data to database, you can try the createmethod from the model:

因为您有一个数组来将数据插入数据库,所以您可以尝试create模型中的方法:

Cylinder::create($array);

but it actually needs the keyof the array to be the field_namein your database. Or you can do this with the query builder:

但它实际上需要在您的数据库key中的数组field_name。或者您可以使用查询构建器执行此操作:

DB::table('table_name')->insert($array);

and again it is required to set the keyof the array to be the field_namein your database.

再次需要key将数组的设置为field_name数据库中的 。