根据其他列值计算列值

时间:2015-07-22 01:08:37

标签: php laravel eloquent

我需要根据同一个表中的其他列值计算列值,但无法在文档中找到方法。我的代码如下:

$toChange = json_decode($request->toSend);
        $database = $request->user()->id + 1;
        Config::set('database.connections.usertable.database','usertable' . $database );
        $insert = new Products();
        foreach ($request->countries as $key => $value) {
            if ($value === true) {
                foreach ($toChange as $change) {
                    $insert->setTable($key . '_products');
                    $insert->whereBetween('price',[$change->priceRangeFrom,$change->priceRangeTo])->update([
                        'ip_address' => $request->getClientIp(),
                        'opt_active' => 1,
                        // next line is the problem
                        'price' => 'price' / 100 * (100 - $change->minPrice) 
                    ]);

                }

            }
        }

我用它更新了几千个条目,需要从每一列的价格字段中获取值,并从中计算新价格。有人可以建议怎么做吗?

查询应该像这个pdo查询

$stmt = $pdo->prepare("UPDATE uk_products SET min_price = (price * ((100 - :min) / 100)) /* ... */ WHERE price BETWEEN :minrange AND :maxrange");
                $stmt->execute([':min' => $data['min'] /* ... */ ]);

这是有问题的计算

min_price = (price * ((100 - :min) / 100)) 

2 个答案:

答案 0 :(得分:0)

这将有效,以防其他人需要它

'min_price' => DB::raw('price / 100 * (100 - '.$change->minPrice.')')

答案 1 :(得分:0)

为什么不预先计算该值并将最终值传递给PDo语句。

*** LOCAL GEMS ***                                                                                        

atk (2.2.5 x64-mingw32)                                                                                   
bigdecimal (1.2.4)                                                                                        
cairo (1.14.1 x64-mingw32)                                                                                
cairo-gobject (2.2.5 x64-mingw32)                                                                         
gdk3 (2.2.5 x64-mingw32)                                                                                  
gdk_pixbuf2 (2.2.5 x64-mingw32)                                                                           
gio2 (2.2.5 x64-mingw32)                                                                                  
glib2 (2.2.5 x64-mingw32)                                                                                 
gobject-introspection (2.2.5 x64-mingw32)                                                                 
gtk2 (2.2.5 x64-mingw32)                                                                                  
io-console (0.4.3)                                                                                        
json (1.8.1)                                                                                              
minitest (4.7.5)                                                                                          
pango (2.2.5 x64-mingw32)                                                                                 
pkg-config (1.1.6)                                                                                        
psych (2.0.5)                                                                                             
rake (10.1.0)                                                                                             
rdoc (4.1.0)                                                                                              
require_all (1.3.2)                                                                                       
test-unit (2.1.6.0)                                                                                       
vrlib (1.0.16)