Cowgirl
Cowgirl

Reputation: 1494

foreach function accessing multiple variables

I am using Laravel, and I have a foreach function like this

$name = person::find(1);
foreach($name as $dbitems)  {
            $person->services()->updateOrCreate([
                'service' => $dbitems->Name,
                'time' => $dbitems->Time
                'price' => $anotherarrayhere
                ]);
        }

In the place of $anotherarrayhere, I want to get this array from the outside of foreach function.

$anotherarrayhere = $peopleserviceprice[]

How can I include the $anotherarrayhere variable in the above foreach function?

Upvotes: 0

Views: 70

Answers (1)

Lloople
Lloople

Reputation: 1844

foreach is not a function, you can use variables from outside the loop without problems. Maybe you're confused with collection callbacks that needs to specific tell variables

Any way, you're iterating over a model, not a collection, since you're using find.

$person = person::find(1);
$person->services()->updateOrCreate([
    'service' => $person->name,
    'time' => $person->time
    'price' => $anotherarrayhere
]);

Upvotes: 2

Related Questions