我试图将数据传递给我的视图,但我有多维数组中的数据。
我可以迭代它但我认为使用多个foreach
不是一个好方法因为我必须使用foreach
循环为每个单独的数组迭代它。
有没有其他清洁方式?
控制器方法代码
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}}]}]
我不知道它会起作用。但你可以使用像这样的连接。
$result = privatejobcity::join(
'private_jobadb',
'privatejobcity.private_jobabd_id', '=', 'private_jobadb.id'
)->with('cities')
->where('privatejobcity.city_id', $job_test->id)
->get();
如果您发布表格详细信息,我们可以轻松回答您的问题。