我有两个数组,我想在循环中使用它我怎么做呢
控制器代码是:
public function showJobCategoryContent($id)
{
$jobsInfoById = DB::table('jobs')->where('category_id', '=', $id)->where('published', '=', 1)->paginate(3);
$jobsInfoById = $jobsInfoById->map(function ($job) {
return [
'created_at' => Carbon::parse($job->created_at)->addDays(30)->toDateTimeString()
];
});
$dates = [];
$dates[] = $jobsInfoById->pluck('created_at');
$jobsInfosById = [];
$jobsInfosById[] = DB::table('jobs')->where('category_id', '=', $id)->where('published', '=', 1)->paginate(3);
return view('front.category-content.job-category-content',[
'jobsInfosById'=> $jobsInfosById,
'dates'=>$dates
]);
}
如何在循环中使用这两个数组
我的观点页面:
@php
$arrayData = array_merge($jobsInfosById, $dates);
@endphp
@foreach($arrayData as $jobInfoById)
<li>
<h4>{{$jobInfoById->company_name}}</h4>
<h5 class="deadline">Deadline:<b>{{ date('d F, Y', strtotime($jobInfoById->date)) }}</b></h5>
</li>
@endforeach
显示错误未定义属性:
照亮\分页\ LengthAwarePaginator :: $ COMPANY_NAME
{ $ arrayData值如下:
阵 ( [0] =&gt; Illuminate \ Pagination \ LengthAwarePaginator对象 ( [总计:受保护] =&gt; 3 [lastPage:protected] =&gt; 1 [items:protected] =&gt;照亮\支持\集合对象 ( [items:protected] =&gt;排列 ( [0] =&gt; stdClass对象 ( [id] =&gt; 3
[company_name] => Khulan Computer house
[published] => 1
[created_at] => 2017-10-22 15:25:54
[updated_at] => 2017-10-24 11:00:52
)
[1] => stdClass Object
(
[id] => 4
[company_name] => Khulan Computer house
[published] => 1
[created_at] => 2017-10-22 15:46:44
[updated_at] => 2017-10-24 11:00:41
)
[2] => stdClass Object
(
[id] => 11
[company_name] => Computer Lab
[published] => 1
[created_at] => 2017-10-24 08:01:00
[updated_at] => 2017-10-24 10:53:57
)
)
)
[perPage:protected] => 3
[currentPage:protected] => 1
[path:protected] => http://localhost/jobs-zone/public/category-content/job-category-content/4
[query:protected] => Array
(
)
[fragment:protected] =>
[pageName:protected] => page
)
[1] => Illuminate\Support\Collection Object
(
[items:protected] => Array
(
[0] => 2017-11-21 15:25:54
[1] => 2017-11-21 15:46:44
[2] => 2017-11-23 08:01:00
)
)
)[![在此输入图像说明] [1]] [1]
[1]:
答案 0 :(得分:0)
你应该试试这个:
更改您的视图代码可能会返回数组。
@foreach($arrayData as $jobInfoById)
print('<pre style="color:red;">');
print_r($jobInfoById);
print('</pre>');
exit;
<li>
<h4>{{$jobInfoById['company_name']}}</h4>
<h5 class="deadline">Deadline:<b>{{ date('d F, Y', strtotime($jobInfoById->date)) }}</b></h5>
</li>
@endforeach
答案 1 :(得分:0)
您正在创建一个数组并在数组的元素0上进行分配,并且您执行了2次查询。所以我的想法是:
public function showJobCategoryContent($id)
{
$dates = DB::table('jobs')->select('created_at')->where('category_id', '=', $id)->where('published', '=', 1)->paginate(3);
$jobsInfosById = DB::table('jobs')->where('category_id', '=', $id)->where('published', '=', 1)->paginate(3);
return view('front.category-content.job-category-content',[
'jobsInfosById'=> $jobsInfosById,
'dates' => $dates
]);
}