Haider
Haider

Reputation: 55

How to use foreach loops in one foreach loop

How to use two foreach loops in one foreach loop.

foreach ($user_package as $package_id)
{
    $package_detail = Package::all()->where('id','=',$package_id->package_id);
}

foreach ($package_detail as $package)
{
    $package_amount = $package->price;
    $package_tagline = $package->tagline;
}

Upvotes: 0

Views: 143

Answers (1)

Jaseel P V
Jaseel P V

Reputation: 310

You can pluck the id's to an array

$userPackageIds = UserPackage::pluck('package_id')->toArray();

im assuming your model name is UserPackage.

And retrieve all packages using the ids

$package_detail = Package::whereIn('id',$userPackageIds);

Your code will be like this

$userPackageIds = UserPackage::pluck('package_id')->toArray();
$package_detail = Package::whereIn('id',$userPackageIds);

foreach( $package_detail as $package ){
    $package_amount = $package->price;
    $package_tagline = $package->tagline;
}

Upvotes: 3

Related Questions