我在我的draws
表中插入了大量数据。在我这样做的同时,我需要将 $pools = DB::table('addresses as a')
->join('incodes as i', 'i.id', '=', 'a.incode_id')
->join('draws as d', 'd.incode_id', '=', 'i.id')
->join('users as u', 'u.id', '=', 'a.user_id')
->where('d.isactive', '=', '1')
->select('u.id as user_id', 'd.id as draw_id')->distinct();
$bindings = $pools->getBindings();
$insertQuery = 'INSERT into pools (user_id, draw_id)'.$pools->toSql();
$pools = \DB::statement($insertQuery, $bindings);
foreach($pools as $pool) {
//Deactivate draw id from draws table when
//successfully inserted in pools table
Draw::where('id', $pool->draw_id)
->update(['isactive' => 0]);
}
表中的 isactive 字段设置为 0 。但是我遇到了这样的问题。
代码:
foreach
由于$pools
的自然,\DB::statement
循环永远不会与isactive
一起使用。
那么当draws
表中的0
字段插入pools
表时,如何将{{1}}字段设置为{{1}}?
由于
答案 0 :(得分:1)
试试这个:
Config::set('database.fetch', PDO::FETCH_ASSOC);
$pools = DB::table('addresses as a')
->join('incodes as i', 'i.id', '=', 'a.incode_id')
->join('draws as d', 'd.incode_id', '=', 'i.id')
->join('users as u', 'u.id', '=', 'a.user_id')
->where('d.isactive', '=', '1')
->select('u.id as user_id', 'd.id as draw_id')
->distinct()
->get();
Pool::insert($pools);
foreach ($pools as $pool) {
if ($draw = Draw::find($pool['draw_id'])) {
$draw->isactive = 0;
$draw->save();
}
}