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

Jignesh Joisar
  • 13,720
  • 5
  • 57
  • 57
Entrepreuner
  • 53
  • 4
  • 11

3 Answers3

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.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
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

Jignesh Joisar
  • 13,720
  • 5
  • 57
  • 57
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).

Prashant Deshmukh.....
  • 2,244
  • 1
  • 9
  • 11