user3485335 user3485335 - 5 months ago 19
SQL Question

DB Query on returning one value Laravel

Im using a simple query to pull the users in a table called users.

$users = DB::table('users')->get();


Then using a foreach loop to get the values , for example :

foreach ($users as $user)
{
return $user->email;
}


There is definitely 2 records in the DB Table, but it only returns one line. If i try to query a different table it does the same thing.

If i use the Query Log between the query

DB::enableQueryLog();
$users = DB::table('users')->get();
$result = DB::getQueryLog();


It returns the query and it looks fine

[{"query":"select * from `users`","bindings":[],"time":0.85}]


This table was created within PhpMyAdmin and not using the Artisan Migrate

Answer

Of course it so, cause you return from loop, use echo

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

return statement inside the loop will behave like break for your case, this means breaking the loop.