如何从tosql函数中检索id

时间:2015-07-15 15:53:50

标签: php laravel laravel-5

我在我的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}}?

由于

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();
    }
}