There are two fundamental things very wrong with your queries:
-
->get() fetches the rows from the database, creates an Eloquent Collection object with those rows and returns that collection. That means everything you do after ->get() is not part of the query and never goes to the database – it’s instead a function used to filter the collection. Any database function calls need to come before ->get().
-
->where() (both for database queries and collection filtering) requires at least two arguments: one to specify which column you’re filtering by, and one to specify which value that column should have. ->where($user) is equivalent to writing in SQL WHERE $user, which will evaluate $user as a boolean value and return either all rows or no rows.
Also, auth()->user() returns an object for the current user. In quite a few places in Laravel, you can pass an object and Laravel will know to use the primary key in that object, but it’s generally better practice not to rely on that, because it doesn’t work everywhere. For instance, it doesn’t work in the ->where() function in collections.
So, Setting::get()->where(auth()->user()) does this:
- Retrieves all records from the
settings table as a collection
- Filters that collection by a column named… whatever the string value of a user object is (basically “Filter the collection and only return elements where [Object]”, which makes no sense)
What you want to do is filter in the database (by putting where() before get()), and of course tell the database what column you’re trying to filter on as well (by using both parameters to the where() function). Since you say (further below) that there’s only ever one row per user, you also don’t want to retrieve a whole collection of objects – you just need a single model instance, so use first() instead of get():
$user = auth()->user();
$settings = Setting::where('user_id', $user->id)->first();
$cards = Card::where('user_id', $user->id)->first();
return view('user/dashboard', compact('user', 'settings', 'cards'));