0

I am trying to find the number of user with WhereIN query, I tested with three types 1st two return user correctly but when I use the variable instead of direct data then it returns null

$userss = User::where('id',Auth::user()->id)->first();

        $membership = Ppv::whereIN('membership_id',[3])->count();
       dd( $membership);//its returns user correctly



 $membership = Ppv::where('membership_id',$userss->membership_id)->count();
           dd( $membership); // also return users correctly

but when i use it in whereIN query it returns null

$userss = User::where('id',Auth::user()->id)->first();
    $membership = Ppv::whereIN('membership_id',array($userss->membership_id))->count();
   dd( $membership); // retruns null or 0 
5
  • 1
    Did you mean whereIn() and not whereIN()? Commented Jul 7, 2020 at 11:28
  • I don't understand why you would use whereIn for an array with one item. Commented Jul 7, 2020 at 11:35
  • @rkg i am consudering both same. both are not working as well Commented Jul 7, 2020 at 11:38
  • @Dan because membership_ids are stored in 3,5,7 Commented Jul 7, 2020 at 11:39
  • @Dan in membership_id comlum values are stored in array format with implode Commented Jul 7, 2020 at 11:41

2 Answers 2

3

You are using $userss, like it is an id. It is not. It is a User model. If you want to use it how you described, you need:

    $membership = Ppv::whereIn('membership_id',[ $userss->membership_id ])->count();

But if you are going to take first(), from User, you should just do:

    $membership = Ppv::where('membership_id',$userss->membership_id)->count();
Sign up to request clarification or add additional context in comments.

5 Comments

not worked i also tried i am also confused. but when i insert data directly it works
Please edit your question with your solution please, to help other users.
your last update also not fits because i need in arrray,, but thanks alot , i will always remeber it
It is an array: whereIn('membership_id',[ $userss->membership_id ])
Post your solution
0

$user = User::where('id',Auth::user()->id)->first();

Please check the value of $user->membership_id;

return Ppv::whereIN('membership_id',[$user->membership_id])->count();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.