简体   繁体   中英

Save all record records using request->all in Laravel

Laravel provides a great help for developers to save all input fields of a form which is one record with one line of code.

like if I want to save a form which has multiple input fields and one record to database like:

在此处输入图像描述

then I can save it with below code and it works great:

SaveOrder:: create($request->all());

Now I have a question. If I have multiple records (multiple rows) in a form and I can add new rows with a button pressed. Then how can I save all records with above code?

Like: 在此处输入图像描述

It's easy to do that using Eloquent :

$data = array(
    array('field1'=>'value1', 'field2'=> value2),
    array('field1'=>'value1', 'field2'=> value1),
    //...
);

Model::insert($data);

Assuming your input names look something like name[] , since you can add rows on the fly, you can retrieve the input as an array, and insert them using something like this:

$data = [];

$names = request('name');
$product_names = request('product_name');
$product_colour = request('product_colour');
$product_size = request('product_size');

for ($i = 0; $i < count($names); $i++) {
    // Add checks to make sure indices actually exist, probably using preprocessing in JS
    $data[] = [
        'name' => $names[$i],
        'product_name' => $product_names[$i],
        'product_colour' => $product_colour[$i],
        'product_size' => $product_size[$i],
    ];
}

Model::insert($data);

The best answer for this question is using foreach statement. Like:

    $CustomerName= $request -> input('CustomerName');
    $ProductId= $request -> input('ProductId');
    $ProductName= $request -> input('ProductName');
    $ProductColor= $request -> input('ProductColor');

    foreach( $ProductId as $key => $n ) {
        SaveOrder::insert(
                    array(
                        'CustomerName' => $CustomerName[$key],
                        'ProductId' => $ProductId[$key],
                        'ProductName' => $ProductPrice[$key],
                        'ProductColor' => $ProductQuantity[$key],
                    )
                    );}

Use upsert

If you use Laravel 8 or above, you can make use of upsert . Such an useful function to insert or update matching records at the same time.

SaveOrder::upsert($request->all(), ['id'], ['CustomerName', 'ProductName', 'ProductColor', 'ProductID']);

The method's first argument consists of the values to insert or update, while the second argument lists the column(s) that uniquely identify records within the associated table. The method's third and final argument is an array of the columns that should be updated if a matching record already exists in the database. The upsert method will automatically set the created_at and updated_at timestamps if timestamps are enabled on the model:

Flight::upsert([
  ['departure' => 'Oakland', 'destination' => 'San Diego', 'price' => 99],
  ['departure' => 'Chicago', 'destination' => 'New York', 'price' => 150]
], ['departure', 'destination'], ['price']);

Read the documentation on Laravel Upsert

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