Php Laravel:添加到列中的现有字段数据
请告诉我如何用雄辩的语言表达这个php mysql更新代码Php Laravel:添加到列中的现有字段数据,php,mysql,orm,laravel,eloquent,Php,Mysql,Orm,Laravel,Eloquent,请告诉我如何用雄辩的语言表达这个php mysql更新代码 mysql_query("UPDATE `some_table` SET `value` = `value` + 1000 WHERE `id` = 1"); 或 您只需检索模型并将其递增: $model = Some_Model::find( $id ); $model->value += 1000; $model->save(); 使用Eloquent,您可以按如下方式编写查询: SomeTable::where('
mysql_query("UPDATE `some_table` SET `value` = `value` + 1000 WHERE `id` = 1");
或
您只需检索模型并将其递增:
$model = Some_Model::find( $id );
$model->value += 1000;
$model->save();
使用Eloquent,您可以按如下方式编写查询:
SomeTable::where('id',1)
->更新(数组('value',DB::raw('value+1000'));
实现这一点的理想方法是使用内置的Laravel函数增量
$model = Some_Model::find( $id );
$model->increment('value',1000);
或
相同的文档位于这里是我的更新代码Inventorie::update($arrinput['id'][$i],数组('quantity'=>'quantity+'$arrinput['q'][$i],'cprice'=>$arrinput['c'][$i],'sprice'=>$arrinput['s'][$i]);回显“已更新”。
代码>这是我的更新代码inventory::update($arrinput['id'][$i],数组('quantity'=>'quantity+'$arrinput['q'][$i],'cprice'=>$arrinput['c'][$i],'sprice'=>$arrinput['s'][$i]);回显“已更新”。
您遗漏了更新的DB::raw()
部分。这很重要,因为否则它会将“数量”视为字符串而不是列名。您希望它是'quantity'=>DB::raw('quantity+'.$arrinput['q'][$i])
OMG!你是最棒的!它就像魔术一样!!!,非常感谢你的帮助!!!,我可以用同样的方法执行其他算术函数吗?DB::raw()
将告诉它直接将内容传递给数据库引擎。您可以在laravel.com/docs上的查询生成器下阅读
$model = Some_Model::find( $id );
$model->increment('value',1000);
Some_Model::where('id',1)->increment('value',1000);