13garth
13garth

Reputation: 783

Laravel Database Query Not Returning Data

The Query

$user_id = Auth::id();
$group_users = GroupUser::where('user_id', $user_id);
return view('groups.index', ['group_users' => $group_users]);

My Query has no errors

I need to list all rows from the group_users table with the the user ID that is equal to the

$user_id = Auth::id();

I am including the model with

use App\GroupUser;

And my table I am retrieving the data from has the following fields,

- group_id
- user_id
- user_role

And if I echo or print the

$user_id = Auth::id();

I get the correct user id. I'm not sure what I am missing.

Upvotes: 0

Views: 1241

Answers (2)

LegoBoy
LegoBoy

Reputation: 140

GroupUser::where('user_id', $user_id) just return a builder.If you want return data you can fix :

$group_users = GroupUser::where('user_id', $user_id)->first(); to return first data

or

$group_users = GroupUser::where('user_id', $user_id)->get(); to return all data of table GroupUser

Upvotes: 0

u_mulder
u_mulder

Reputation: 54831

GroupUser::where does not execute a query. To get results add get() chaining method:

$group_users = GroupUser::where('user_id', $user_id)->get();

Upvotes: 3

Related Questions