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

Abdullah_Iftikhar's avatar

Help me in foreach loop scope of variable with redirect to view.

I want to use the "$referral_user_2" this variable out of scope of foreach loop because i need this data in my view therefor i want to save in globally variable and return view()->with() by this method. How can i return this for loop variable to my view ?

public function index() {

    $referral_user_1 = ReferralUser::where('referral_id' ,Auth::user()->id)->where('status' ,1)->get();

    foreach ($referral_user_1 as $first_referral) {

        $referral_user_2 = ReferralUser::where('referral_id' , $first_referral->user_id)->where('status' ,1)->get();

        dd($referral_user_2);

    }

}

"I want get referral from referral table.

like get referral from id=1 user

id=22 is referral user_id = 1 id=23 is referral user_id = 1

so then

id=24 is referral user_id = 22 id=25 is referral user_id = 22

id=26 is referral user_id = 25 id=27 is referral user_id = 25

like this type of logic"

0 likes
12 replies
Nakov's avatar

You initialize it out of the loop:

$referral_user_2 = new ReferralUser;
foreach ($referral_user_1 as $first_referral) {

    $referral_user_2 = ReferralUser::where('referral_id' , $first_referral->user_id)->where('status' ,1)->get();

}

dd($referral_user_2); // you can access it out of the loop.

Btw, using ->get() will return a collection, not a single ReferralUser, you can use ->first() if you want to select just one.

Snapey's avatar

you need to keep pushing to the variable as an array. otherwise it will only contain the outcome of the last loop.

towhid's avatar

user this code this file app/Providers/AppServiceProvider.php


public function boot()
{
   // add this
$referral_user_2 = ReferralUser::where('referral_id' , $first_referral->user_id)->where('status' ,1)->get();
    view()->share('referral_user_2',$referral_user_2);
}

try this one

Abdullah_Iftikhar's avatar

No i want to select collection basically it's a referral controller.... I want get referral from referral table.

like get referral from id=1 user

id=22 is referral user_id = 1 id=23 is referral user_id = 1

so then

id=24 is referral user_id = 22 id=25 is referral user_id = 22

id=26 is referral user_id = 25 id=27 is referral user_id = 25

like this type of logic

Nakov's avatar

@abdullah_iftikhar you should read the comments:

$referralUsers = [];
foreach ($referral_user_1 as $first_referral) {

    $referralUsers[] = ReferralUser::where('referral_id' , $first_referral->user_id)->where('status' ,1)->get();

}

return view('your_view')->with('referralUsers', $referralUsers);

This does not work?

1 like
Abdullah_Iftikhar's avatar

I try all.

  1. "$referral_user_1" have 2 collection for example id=22 and id=23

when i try this dd($refferal_user_2); in foreach loop then it's return the data for id=22....... for example id=24 and id=25 and referral_id = 22

foreach ($referral_user_1 as $first_referral) {

        $refferal_user_2 = ReferralUser::where('referral_id' , $first_referral->user_id)->where('status' ,1)->get();
        dd($refferal_user_2);
  }

when i try dd($refferal_user_2); out of loop scope the it's return the data for id=23 and 23 have 1 record like id=26 and referral_id = 23...

How can i get the exact referral.

Nakov's avatar
Nakov
Best Answer
Level 73

@abdullah_iftikhar because you are overriding the result ALWAYS.. take a look at my second example, the most recent one, where I use array as Snapey suggested..

$referralUsers = [];
foreach ($referral_user_1 as $first_referral) {

    $referralUsers[] = ReferralUser::where('referral_id' , $first_referral->user_id)->where('status' ,1)->get();

}

dd($referralUsers); // array of referrals.
1 like
Abdullah_Iftikhar's avatar

Second example is very help full for me and i found my result using second example Thank you so much @nakov .

Please or to participate in this conversation.