$res = DB::table('shop')->get()->toArray(); //查出所有文章记录 $cate = DB::table('shop_cate')->get()->toArray(); // 查出栏目所有记录 $cates = []; foreach ($cate as $val) { $cates[$val->id] = $val->title; //以栏目id为下标 栏目名称为值 重新组一个新数组 } foreach ($res as $key => $val){ $res[$key] = (array)$val; //强制把对象转换为数组 } return view('product.lists',['product'=>$res,'cate_title'=>$cates]); //传入视图
@foreach($product as $item) <tr> <td>{{$item['id']}}</td> <td>{{$cate_title[$item['cid']]}}</td> <td>{{$item['title']}}</td> </tr> @endforeach