我有一个例程,它读取一系列雄辩的命令(从路由/控制器的输入或通过文件):
例如:
Product::where('code', '=', 'ABC123')->update(array('price' => 123.45));
Product::where('code', '=', 'ABC246')->update(array('price' => 222.99));
我该如何让Laravel执行这些,就好像它自己在建造它们一样?
我知道我可以使用PHP的eval()
方法,但这只是最后的手段。传递的命令是由我们的另一个内部系统生成的,因此利用它没有问题,但我希望有一个比eval()
更好的选项。
我认为你需要在循环中一个接一个地完成
$products = Product::where('code', '=', 'ABC123')->get();
foreach ($products as $product) {
$product->price() = '123.45';
$product->save();
}