2017-09-04 57 views
0

我想將數據傳遞給我的視圖,但我有多維數組中的數據。我可以遍歷它,但我認爲使用多個foreach不是一個好方法,因爲我必須使用每個單個陣列的foreach循環遍歷它。有沒有其他更清潔的方式?前如何在Laravel中的多維數組中有數據時傳遞數據?

控制器方法代碼

public function city(city $job_test) 
    { 

     $data = privatejobcity::where('city_id', $job_test->id)->get(); 

     $private_job = []; 
     foreach ($data as $values) { 

      echo "<pre>"; 
      $private_job[] = private_jobadb::with('cities')->where('id', $values->private_jobabd_id)->get(); 
     } 


     foreach ($private_job as $values1) { 

      echo "<pre>"; 
      echo $values1; 


     } 


     die(); 
     return view('frontend.jobTestCatagory.jobsByCities', compact('private_job')); 

結果die()

[{"id":31,"company_logo":null,"company_name":null,"job_desc":null,"company_phone":null,"sector_id":null,"company_eail":null,"created_at":"2017-09-03 04:38:50","updated_at":"2017-09-03 04:38:50","image":"public\/newpaper_jobs\/AAEAAQAAAAAAAAaHAAAAJDhmMTBjZmJlLWUxNmQtNDE3Zi1iY2Y5LTY1OWI1MzkwNzQ1YQ.png","contact_person":null,"job_title":null,"slug":null,"meta_tags":null,"meta_description":null,"cities":[{"id":4,"city_name":"Islamabad","pivot":{"private_jobabd_id":31,"city_id":4}}]}] 
[{"id":32,"company_logo":null,"company_name":null,"job_desc":null,"company_phone":null,"sector_id":null,"company_eail":null,"created_at":"2017-09-03 04:44:09","updated_at":"2017-09-03 04:44:09","image":null,"contact_person":null,"job_title":null,"slug":null,"meta_tags":null,"meta_description":null,"cities":[{"id":4,"city_name":"Islamabad","pivot":{"private_jobabd_id":32,"city_id":4}}]}] 
[{"id":33,"company_logo":null,"company_name":null,"job_desc":null,"company_phone":null,"sector_id":null,"company_eail":null,"created_at":"2017-09-03 04:50:28","updated_at":"2017-09-03 04:50:28","image":"public\/newpaper_jobs\/241_District-Council-Bahawalnagar.jpg","contact_person":null,"job_title":null,"slug":null,"meta_tags":null,"meta_description":null,"cities":[{"id":4,"city_name":"Islamabad","pivot":{"private_jobabd_id":33,"city_id":4}}]}] 
[{"id":46,"company_logo":null,"company_name":null,"job_desc":null,"company_phone":null,"sector_id":1,"company_eail":null,"created_at":"2017-09-03 06:53:46","updated_at":"2017-09-03 06:53:46","image":null,"contact_person":null,"job_title":null,"slug":"943","meta_tags":null,"meta_description":null,"cities":[{"id":4,"city_name":"Islamabad","pivot":{"private_jobabd_id":46,"city_id":4}},{"id":3,"city_name":"Karachi","pivot":{"private_jobabd_id":46,"city_id":3}}]}] 
+0

通過'private_jobadb'加入'privatejobcity'並將你的多維數組傳遞給你查看對你來說不錯。然後在你的視圖中迭代數組。 – C2486

回答

0

我不知道它會工作或沒有。但你可以使用像這樣的加入。

$result = privatejobcity::join(
     'private_jobadb', 
     'privatejobcity.private_jobabd_id', '=', 'private_jobadb.id' 
    )->with('cities') 
    ->where('privatejobcity.city_id', $job_test->id) 
    ->get(); 

如果您發佈您的表格詳細信息,我們可以很容易地回答您的問題。