2016-03-28 7 views
1

mit Ich möchte die folgende SQL-Anweisung in ein Eloquent Format neu zu schreiben:Equivalent für WHERE-Klausel Eloquent Funktion

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

Mein Gedanke war

public function getDevicesWithIDs($ids) { 

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

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

Aber dies zurück:

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

Antwort

3

wenn $ids ein Array

ist 0

Sie können Ihr Ergebnis mit etwas wie unten

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

+0

ausgezeichnet erhalten! Vielen Dank! – sesc360