Home > Net >  Laravel where clause returns only one db entry
Laravel where clause returns only one db entry

Time:06-07

I'm trying to return multiple lines from my DB using Laravel. The lines of the DB are as following:

 ---- -------------------- ----------- -------------- -------- --------- 
| id | drank              | categorie | besteld_door | aantal | status  |
 ---- -------------------- ----------- -------------- -------- --------- 
|  1 | Cola               | Frisdrank | Pieter       |      1 | Besteld |
|  2 | Grolsch            | Bier      | Henk         |      1 | Besteld |
|  3 | Cabernet Sauvignon | Wijn      | Mark         |      3 | Besteld |
|  4 | Heineken           | Bier      | Freek        |      1 | Besteld |
|  5 | Heineken           | Bier      | Pieter       |      1 | Besteld |
 ---- -------------------- ----------- -------------- -------- --------- 

I'm trying to get all lines where the column 'besteld_door' is 'Pieter', however my Laravel code (see below) is only returning the first entry..

$user = 'Pieter';
$bestelling = Drink::where('besteld_door', $user)->value('drank'); // --> returns 'Cola'

Can anyone help me out here?

CodePudding user response:

If you want to get all the value of "drank" use pluck()

$bestelling = Drink::where('besteld_door', $user)->pluck('drank');
//if you want the array use ->toArray()
$bestellingArray = $bestelling->toArray();

If you want a collection of the models and not just the column "drank" use get()

$bestelling = Drink::where('besteld_door', $user)->get();
//you can then loop the results
foreach ($bestelling as $drink) {
    echo $drink->drank;
}
  • Related