Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

AndySong's avatar

How can I return boolean from sql result

course_user is a pivot table, How can I know if there is a result that match the condition.

\DB::table('course_user')->where([
            ['course_id', '=', $course->id],
            ['user_id', '=', auth()->id()],
        ])

I mean how can I return it as true or false ! Thanks

0 likes
3 replies
KNietzsche's avatar
Level 17

something like that

return $result = DB::table('YOUR_TABLE')->where('FIELD','OP','VALUE')->get()->count();
thoasty's avatar
/**
     * Determine if any rows exist for the current query.
     *
     * @return bool
     */
    public function exists()

@AndySong Do not use ->get()->count() if you only want to retrieve the count.

3 likes
KNietzsche's avatar

I do agree with @thoasty

return $result = DB::table('YOUR_TABLE')->where('FIELD','OP','VALUE')->exists();

seems better, I learned something too, tks

3 likes

Please or to participate in this conversation.