0

I have two 3 tables : users,profiles, friend_request

$my_profile_id variable store the value of user's profile ID

$my_user_id = Auth::user()->id;
$my_profile_id =   DB::table('profiles')->select('profiles.profile_id', 'profiles.id')->leftjoin('users','users.id' ,'=' ,'profiles.id')->where('users.id',$my_user_id)->pluck('profiles.profile_id')->first();

$friend_accept = DB::table('friend_request')->select('profiles.profile_id','profiles.first_name','interest_request.sent_at','interest_request.interest_id')->leftjoin('profiles', function($join){
$join->on('interest_request.sender_id' , '=','profiles.profile_id');
$join->orOn('interest_request.to_user_id' , '=','profiles.profile_id');
 })->where('to_user_id',$my_profile_id)->orwhere('sender_id',$my_profile_id)->where('interest_status','2')->whereNotIn('profiles.profile_id', function($q)
  {
   $q->select('profiles.profile_id')->from('profiles')->where('profiles.profile_id',$my_profile_id);
   })->groupby('profiles.profile_id')->get(); 

$my_profile_id variable is working fine in where and orwhere clause but when I use in whereNotIn sub query clause it create error: Variable is not defined

3 Answers 3

1

While your code style is a little hard to read, It looks like you are missing a step. However, my preferred code style has already been put down in a reply with probably working code. Don't make live to hard on your self and follow that code style.

Your code:

whereNotIn('profiles.profile_id', function($q)
{

Posted code:

whereNotIn('profiles.profile_id', function($q) use ($my_profile_id) {

What happened was you missed a complete instruction on what to use.

Sign up to request clarification or add additional context in comments.

Comments

0

u create a closure but did not pass parameter into Closure so your code is not working

since inside the closure PHP doesn't know the $my_profile_id variable you need to have a use statement in the closure too

$my_user_id = Auth::user()->id;
$my_profile_id =   DB::table('profiles')->select('profiles.profile_id', 'profiles.id')->leftjoin('users','users.id' ,'=' ,'profiles.id')->where('users.id',$my_user_id)->pluck('profiles.profile_id')->first();

$friend_accept = DB::table('friend_request')->select('profiles.profile_id','profiles.first_name','interest_request.sent_at','interest_request.interest_id')->leftjoin('profiles', function($join){
$join->on('interest_request.sender_id' , '=','profiles.profile_id');
$join->orOn('interest_request.to_user_id' , '=','profiles.profile_id');
 })->where('to_user_id',$my_profile_id)
   ->orwhere('sender_id',$my_profile_id)
   ->where('interest_status','2')
   ->whereNotIn('profiles.profile_id', function($q) use ($my_profile_id) {
           $q->select('profiles.profile_id')->from('profiles')
                ->where('profiles.profile_id',$my_profile_id);
   })->groupby('profiles.profile_id')->get(); 

for more info about Closure see

2 Comments

Its working, can you please tell me why my code is not working without use ($my_profile_id)
@Entrepreuner ya i updated my answer with clarification
0

I guess you are not passing $my_profile_id variable inside closure... That`s why it showing variable is undefined

$my_user_id = Auth::user()->id;
$my_profile_id =   DB::table('profiles')->select('profiles.profile_id', 'profiles.id')->leftjoin('users','users.id' ,'=' ,'profiles.id')->where('users.id',$my_user_id)->pluck('profiles.profile_id')->first();

$friend_accept = DB::table('friend_request')->select('profiles.profile_id','profiles.first_name','interest_request.sent_at','interest_request.interest_id')->leftjoin('profiles', function($join){
$join->on('interest_request.sender_id' , '=','profiles.profile_id');
$join->orOn('interest_request.to_user_id' , '=','profiles.profile_id');
})->where('to_user_id',$my_profile_id)->orwhere('sender_id',$my_profile_id)->where('interest_status','2')->whereNotIn('profiles.profile_id', function($q) use ($my_user_id){ $q->select('profiles.profile_id')->from('profiles')->where('profiles.profile_id',$my_profile_id); })->groupby('profiles.profile_id')->get();

Try adding use ($my_user_id) after function($q).

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.