2016-03-28 35 views
1

因爲我想重寫下面的SQL語句轉換成雄辯的格式:等效WHERE子句中使用雄辯的功能

SELECT `id`, `pushbadge`, `pushalert`, `pushsound` 
    FROM `devices` 
    WHERE `id` IN (1, 2, 3) 
     AND `status`='active'" 

我的想法是

public function getDevicesWithIDs($ids) { 

    $conditions = array(); 
    foreach($ids as $id) { 
     $conditions[] = ['id' => $id]; 
    } 
    var_dump($conditions); 

    return Device::where($conditions)->get(); 
} 

但這返回:

SQLSTATE[42S22]: Column not found: 1054 
Unknown column '0' in 'where clause' 
(SQL: select * from `devices` where (`0` = 1)) 

回答

3

if $ids is a array

你可以用的東西讓你的結果如下

Device::whereIn('id', $ids)->where('status', 'active')->get(['id', 'pushbadge', 'pushalert', 'pushsound']);

+0

優秀!謝謝! – sesc360