2017-10-21 70 views
0

我使用Laravel的,我有這樣的的foreach函數訪問多個變量

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

$ anotherarrayhere的地方一個foreach功能,我想從的foreach功能外這個數組。

$anotherarrayhere = $peopleserviceprice[] 

如何在上述foreach函數中包含$ anotherarrayhere變量?

回答

2

foreach是不是一個函數,你可以使用循環外的變量沒有問題。也許你與收集回調混淆,需要具體說明變量

無論如何,你正在遍歷模型,而不是集合,因爲你使用的是find。

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