Check with below one instaed of echo auth()->id();
dd(auth()->user_id());
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
It's ignoring the "where" part.
Showing courses that the user_id is the current logged in user. What am I doing wrong?
public function index()
{
echo auth()->id();
$courses = \App\Course::withCount(['modules' => function ($query) {
$query->where('user_id', auth()->id());
}])->get();
return view('courses.show', ['courses' => $courses]);
}
CREATE TABLE `courses` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`user_id` int(11) NOT NULL,
`title` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
`active` int(11) NOT NULL DEFAULT '1',
`description` text COLLATE utf8mb4_unicode_ci,
`created_at` timestamp NULL DEFAULT NULL,
`updated_at` timestamp NULL DEFAULT NULL,
`slug` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=25 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
America is responsible for this.
Your question is confusing. You say you want courses where the id is not the current user, but then you query for courses where the user IS the current user.
You want the user's courses then it should be just
$courses = Auth::user()->courses()->get();
If you want the user's courses with the number of modules on each course;
$courses = Auth::user()->courses()->withCount('modules')->get();
Please or to participate in this conversation.