6

I can get the first user, who is an admin in Laravel's tinker using the following command:

$adminUser = App\User::where('is_admin',true)->first();

How do I get all the users which meet this where criteria?

Devdatta Tengshe
  • 4,015
  • 10
  • 46
  • 59
  • 2
    `App\User::where('is_admin',true)->get();` – arun May 17 '18 at 07:30
  • first() method will only call single row. get() will call all the matching rows. – Parth Pandya May 17 '18 at 07:31
  • @Arun can you post it as an answer? Is there a document with all such commands? – Devdatta Tengshe May 17 '18 at 07:32
  • laravel documentation is the right place buddy – Parth Pandya May 17 '18 at 07:33
  • @DevdattaTengshe, read these `https://laravel.com/docs/5.6/queries`. eloquent supports all the methods in query builder – arun May 17 '18 at 07:37
  • @DevdattaTengshe Laravel Tinker is an application which is just used to check and test the data. So there is no documentation for that. And as per your question you are just testing so this is common that you don't have to write `$adminUser = App\User::where('is_admin',true)->first();`. just write `App\User::where('is_admin',true)->first();`. I think this is not the answer for this you can delete this question if you find it was common thing. – Chirag Patel May 17 '18 at 07:38

3 Answers3

9

Just change first() to get().

$adminUser = App\User::where('is_admin',true)->get();

first() is used when you want to retrieve single row or column. where get() is used to retrieve all rows.

just go through the laravel official documentation database query builder section here. It will give you information regarding most of every possible things you can playoff with laravel.

Parth Pandya
  • 1,050
  • 7
  • 15
  • 22
2
$users = App\User::where("is_admin", true)->get();

The get method returns an Illuminate\Support\Collection containing the results where each result is an instance of the PHP StdClass object. You may access each column's value by accessing the column as a property of the object:

foreach ($users as $user) {
    echo $user->name;
}

Src: https://laravel.com/docs/5.6/queries

In case of Tinker, things in Query builder and Eloquent docs will work in Tinker. It exists for the purpose of instantly getting the result without debugging using your real application.

Neutral
  • 66
  • 4
0

The first() function is used when you want to retrieve single row, whereas the get() function is used to retrieve all matching rows.

$adminUser = App\User::where('is_admin', true)->get();
dd($adminUser);
fubar
  • 16,918
  • 4
  • 37
  • 43
Kuldeep Mishra
  • 3,846
  • 1
  • 21
  • 26