0

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?

0

1 Answer 1

2

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
]);
Sign up to request clarification or add additional context in comments.

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.