【问题标题】:Increment columns in laravellaravel中的增量列
【发布时间】:2015-07-01 04:57:15
【问题描述】:

有没有办法在 laravel 中增加多于一列?

假设:

DB::table('my_table')
->where('rowID', 1)
->increment('column1', 2)
->increment('column2', 10)
->increment('column3', 13)
->increment('column4', 5);

但这会导致:

Call to a member function increment() on integer

我只是想找到一种使用 laravel 给定函数的有效方法。谢谢。任何建议都可以。

【问题讨论】:

    标签: php mysql laravel increment


    【解决方案1】:

    没有现有的功能可以做到这一点。你必须使用update():

    DB::table('my_table')
       ->where('rowID', 1)
       ->update([
           'column1' => DB::raw('column1 + 2'),
           'column2' => DB::raw('column2 + 10'),
           'column3' => DB::raw('column3 + 13'),
           'column4' => DB::raw('column4 + 5'),
       ]);
    

    【讨论】:

    • 你好@lukasgeiter,如果我有这样的东西怎么办:increment('orders.price', 7/$count, ['orders.amount_paid' => 'John']);其中 $count 是一个定义的变量。我该如何继续?我试图在递增时添加 7
    【解决方案2】:

    Laravel Eloquent 模型中的增量和减量

    添加到购物车选项是电子商务网站中最重要的功能之一。棘手的部分是让购物车中的商品数量显示在购物车图标上。完成这项工作的主要方法是使用 Laravel 上的增量和减量函数。这也有助于从购物车中添加或删除产品。这个功能的实现方式是,

    $user = User::find(‘517c43667db388101e00000f’);
    $user->cart_count++;
    // $user->cart_count--; // for decrement the count
    $user->save()
    

    另一种更简单的方法是,

    $user = User::find($article_id);
    $user->increment('cart_count');
    

    这些也可以:

    $user->increment('cart_count');// increase one count
    $user->decrement('cart_count'); // decrease one count
    $user->increment('cart_count',10); // increase 10 count
    $user->decrement('cart_count',10); // decrease 10 count
    

    【讨论】:

      【解决方案3】:

      为了将来在 5.2 中的参考,它已经通过执行以下操作来实现

      您还可以在操作期间指定要更新的其他列:

      DB::table('users')->increment('votes', 1, ['name' => 'John']);
      

      来源:https://laravel.com/docs/5.2/queries#updates

      【讨论】:

        【解决方案4】:

        首先,根据文档,increment 的结果是一个整数:http://laravel.com/api/4.2/Illuminate/Database/Query/Builder.html

        所以你必须为每个增量进行调用:

        DB::table('my_table')
        ->where('rowID', 1)
        ->increment('column1', 2);
        DB::table('my_table')
        ->where('rowID', 1)
        ->increment('column2', 10);
        DB::table('my_table')
        ->where('rowID', 1)
        ->increment('column3', 13);
        DB::table('my_table')
        ->where('rowID', 1)
        ->increment('column4', 5);
        

        我找不到任何更快的解决方案,除非您想使用原始更新查询命令来解决它。

        此外,您的示例代码可能会生成错误,因为您已使用 ; 关闭语句并在下一行继续新的 ->increment 调用。

        【讨论】:

        • 嗨,感谢您的快速回复。但这有效吗?因为你叫它4次?抱歉,我编辑了关于那个额外分号的帖子
        • 不,看我的更新;原始更新命令更有效。问题是您是否认为这是一种解决方案。
        • 是的,我考虑过,但也许任何其他我可以运行一次的解决方案。
        【解决方案5】:

        现在在 laravel 5.7 laravel query builder, increment and decrement 中,可以轻松完成。

        Model::where('id', "rowID")->increment('columne1');` 
        

        或者你可以使用数据库

        DB::table("my_table")->where('id', "rowID")->increment('column1');
        

        【讨论】:

          【解决方案6】:
          $id=5;
          $votes=20;
          DB::table('users')
             ->where('id', $id)
             ->update([
                 'votes' => DB::raw('votes + '.$votes)
          ]);
          

          【讨论】:

          • 您能否在答案中附加一些评论,以便我们更好地理解您的代码?
          • 虽然此代码可能会回答问题,但提供有关此代码为何和/或如何回答问题的额外上下文可提高其长期价值。
          猜你喜欢
          • 2019-05-16
          • 2022-10-01
          • 2017-12-12
          • 1970-01-01
          • 2019-04-08
          • 2020-06-10
          • 2016-04-29
          • 2018-04-11
          • 2021-08-25
          相关资源
          最近更新 更多