Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get all records which meet a Criteria in Laravel Tinker?

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?

like image 557
Devdatta Tengshe Avatar asked May 17 '18 07:05

Devdatta Tengshe


2 Answers

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.

like image 112
Parth Pandya Avatar answered Nov 09 '22 10:11

Parth Pandya


$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.

like image 22
Neutral Avatar answered Nov 09 '22 08:11

Neutral