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

thespice's avatar

Eloquent - adding to a whereIn array

Hey guys,

I've got the following query that works just fine :

$feeds = DB::table('social_feed')->whereIn('user_id', $following)->where('active', 1)->orderBy('date_posted', 'DESC')->take(100)->get();

The $following variable is an array generated by a previous query, containing a list of user id's.

I would also like to add the current user id to the $feeds query (without altering the $following array). Is this possible?

So in summary, I want to :

Check the 'social_feed' table, find all records where the user_id matches the $following array and the current user id... etc

0 likes
6 replies
tisuchi's avatar

You are almost there. Have you tried like this way?

$following = [12, 21];

DB::table('social_feed')
        ->whereIn('user_id', $following)
        ->where('active', 1)
        ->where('current_user_id', Auth::id()) //it will take logged in user id
        ->orderBy('date_posted', 'DESC')
        ->take(100)->get();
4 likes
GertjanRoke's avatar
Level 4

You could also just merge the current user id and the user ids from the following variable like this:

->whereIn('user_id', array_merge([auth()->id()], $following))

This way they will only be merged in one array without adding the current user id permanent to the $following variable.

2 likes
Snapey's avatar

@GertjanRoke has it, but dont forget the user_id column needs to still be in the whereIn parameters

Snapey's avatar

mark it as the right answer please

Please or to participate in this conversation.