How can I select data rows by id in a database query?

I have a database query that needs filtering further. The id needs to match a number in an array I have. For some reason, I can select where using every column except id. Even something as simple as $query->where('users.id', 1); doesn't work. How can I perform a database query by id?

$portfolio_query_array = UserPortfolio::all()->pluck('user_id')

$query = DB::table('users');
        $query->select('users.id as user_id','users.user_slug', 'users.social_id', 'users.social_img_update_status', 'users.avatar', 'users.first_name', 'users.last_name', 'users.bio', 'comments.rating', 'user_portfolios.title', 'user_portfolios.description', 'user_portfolios.filepath');

        $query->leftjoin('comments', 'comments.comment_to', '=', 'users.id');
        $query->leftjoin('user_portfolios', 'user_portfolios.user_id', '=', 'users.id');

        $query->leftjoin('user_profile_category_selects', 'user_profile_category_selects.user_id', '=', 'users.id');

        $query->where('users.status', 1);
        $query->where('users.user_type', 3);

        // The below simple query line doesn't work
        $query->where('users.id', 1);

        // The lines I do want to work also fail.
        foreach ($portfolio_query_array as $user_id){
            $query = $query + $query->where('users.id', $user_id);
        }

Thank you for your help!


Solution 1:

First of all, you should take advantage of Eloquent ORM, there is no need to use raw queries unless you have some performance issues or really complex queries. Assuming you have 3 models User, UserPortfolio, Comment, and all relationships defined, you could do something as simple as this:

$users = User::with('comments', 'portfolio')
   ->where('status', 1)
   ->where('user_type', 3)
   ->whereHas('portfolio')
   ->get();

In this way, you are getting all users with portfolios which is what I assume you want.

Anyway, if you insist to use a raw query, there whereIn() method is what you need

$portfolio_query_array = UserPortfolio::pluck('user_id')->toArray();

$query = DB::table('users')
    ->select(
         'users.id as user_id',
         'users.user_slug', 
         'users.social_id', 
         'users.social_img_update_status', 
         'users.avatar', 
         'users.first_name', 
         'users.last_name', 
         'users.bio', 
         'comments.rating', 
         'user_portfolios.title', 
         'user_portfolios.description', 
         'user_portfolios.filepath'
     )
     ->leftjoin('comments', 'comments.comment_to', 'users.id')
     ->leftjoin('user_portfolios', 'user_portfolios.user_id', 'users.id')
     ->leftjoin('user_profile_category_selects', 'user_profile_category_selects.user_id', 'users.id')
     ->where('users.status', 1)
     ->where('users.user_type', 3)
     ->whereIn('users.id', $portfolio_query_array)
     ->get();

BTW, why are you doing a leftJoin to user_profile_category_selects if no fields are selected from that table?

In addition, if you are looking for all users that have a portfolio, starting from the whole list of portfolios, wouldn't be better to do a join to portfolios and get only those users having portfolios?

$query = DB::table('users')
    ->select(
         'users.id as user_id',
         'users.user_slug', 
         'users.social_id', 
         'users.social_img_update_status', 
         'users.avatar', 
         'users.first_name', 
         'users.last_name', 
         'users.bio', 
         'comments.rating', 
         'user_portfolios.title', 
         'user_portfolios.description', 
         'user_portfolios.filepath'
     )
     ->leftjoin('comments', 'comments.comment_to', 'users.id')
     ->join('user_portfolios', 'user_portfolios.user_id', 'users.id')
     ->leftjoin('user_profile_category_selects', 'user_profile_category_selects.user_id', 'users.id')
     ->where('users.status', 1)
     ->where('users.user_type', 3)
     ->get();