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

时间:2022-01-05 08:22:14

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

我在POST中的'Cylinders'数据数组中得到一个数组:

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.

我已经尝试了所有我能想到的东西似乎没有用。

/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.

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

3 个解决方案

#1


6  

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_at and updated_at appear 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_at和updated_at将显示在此处。使用Eloquent模型时,这些字段会自动更新。但是,我们不使用Eloquent,因此我们必须手动将值分配给这些字段。

#2


1  

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

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

Cylinder::create($array);

but it actually needs the key of the array to be the field_name in your database. Or you can do this with the query builder:

但它实际上需要数组的键是数据库中的field_name。或者您可以使用查询生成器执行此操作:

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

and again it is required to set the key of the array to be the field_name in your database.

并且还需要将数组的键设置为数据库中的field_name。

#3


1  

I don't know what forced you to use $_POST global 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
    }
}

#1


6  

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_at and updated_at appear 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_at和updated_at将显示在此处。使用Eloquent模型时,这些字段会自动更新。但是,我们不使用Eloquent,因此我们必须手动将值分配给这些字段。

#2


1  

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

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

Cylinder::create($array);

but it actually needs the key of the array to be the field_name in your database. Or you can do this with the query builder:

但它实际上需要数组的键是数据库中的field_name。或者您可以使用查询生成器执行此操作:

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

and again it is required to set the key of the array to be the field_name in your database.

并且还需要将数组的键设置为数据库中的field_name。

#3


1  

I don't know what forced you to use $_POST global 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
    }
}