Laravel 更新多行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16862376/
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 07:50:46  来源:igfitidea点击:

Laravel update multiple rows

phpsql-updatelaravel

提问by Web Student

I am a beginner with Laravel, and I'm stuck with my code. I would like to store my site options and details in the database, and what I am stuck with I don't know how to update multiple rows, maybe my database structure is not the best either.

我是 Laravel 的初学者,我被我的代码困住了。我想将我的站点选项和详细信息存储在数据库中,而我遇到的问题不知道如何更新多行,也许我的数据库结构也不是最好的。

Database

数据库

id | option_name      | option_value
1  | site_name        | Website name
2  | site_slogen      | Website slogen
3  | site_description | Website description
4  | post_per_pages   | 20

My form

我的表格

{{ Form::open('admin/options', 'POST', array('class' => 'span5 no-float centered')) }}

    @foreach($options as $option)

        <?php $name = str_replace('_', ' ', ucfirst($option->option_name)); ?>

        {{ Form::label($name, $name) }}
        @if($option->option_name == "site_description")
            {{ Form::textarea($option->option_name, $option->option_value, array('class' => 'input-block-level', 'rows' => '5')) }}
        @else
            {{ Form::text($option->option_name, $option->option_value, array('class' => 'input-block-level')) }}
        @endif

        {{ $errors->has($option->option_name) ? '<p class="val_error">' .$errors->first($option->option_name). '</p>' : '' }}

        {{ Form::hidden('id[]', $option->id) }}
    @endforeach

     {{ Form::button('Update options', array('class' => 'btn btn-primary btn-block')) }}

I know it's not the best, but I'm trying. Could someone give me a hint with the multiple update?

我知道这不是最好的,但我正在努力。有人可以给我提示多次更新吗?

I tried but I'm lost

我试过了,但我迷路了

回答by Diego Castillo

You could build a query, something like this:

您可以构建一个查询,如下所示:

YourTable::query()->where('YourTableColumn', 'Variable/Field to match against')->update(array('Fiel' => $new_username));

In Update you can pass array of fields with its value which you want to update for all fetched row.

在更新中,您可以传递字段数组及其要为所有获取的行更新的值。

回答by Mr Dung

Oh, i want to know it can affect the number of queries

哦,我想知道它会影响查询的数量

$inputs = Input::all();

foreach($inputs as $key => $value)
{
    $content = Content::find($id);
    $content->$key = $value;
    $content->save();
}

回答by Leonardo Jauregui

Duplicate destination table, name it 'mytable_insertbypass', trunca before operation, then insert optimized, after insert, make a query to update:

复制目标表,命名为'mytable_insertbypass',操作前trunca,然后插入优化,插入后查询更新:

UPDATE products, products_insertbypass SET products.price = products_insertbypass.price WHERE products.id = products_insertbypass;

更新产品,products_insertbypass SET products.price = products_insertbypass.price WHERE products.id = products_insertbypass;

Favaloro rule's ;)

法瓦洛罗规则 ;)

回答by Mr. Sensitive

You can loop through rows and save them.

您可以遍历行并保存它们。

$inputs = Input::all();
$content = Content::find($id);

foreach($inputs as $key => $value)
{
    $content->$key = $value;
}

$content->save();