控制器层中的home.php
<?php
namespace App\Http\Controllers;
use Illuminate\Support\Facades\DB;
class Home extends Controller
{
public function show()
{
//获取文章表的全部信息
$res = DB::table('article')->get()->toArray();
//获取栏目列表名称全部信息
$lister = DB::table('lists')->get()->toArray();
//设置空数组,将栏目数据放入
$cate = [];
//循环栏目对象
foreach($lister as $value){
//空数组的下标就是栏目的id,再增设['title'] 为栏目的title
$cate[$value->id]['title'] = $value->title;
//或者如下,结果同上 ,意思为将对象转换成数组
$cate[$value->id] = (array)$value;
}
//此时空数组 $cate已经放了栏目数组,下标对应栏目id,title对应栏目title
foreach($res as $key=>$value){
//这里将文章内容的二级对象,转换成数组
$res[$key] = (array)$value;
}
//这里的$res 就成为全新的二位数组
//将两个数据全部导入到视图层模板
return view('index', ['index'=>$res, 'lists'=>$cate]);
}
}
视图层: index.blade.php
@foreach($index as $val)
<tr>
<td>{{$val['id']}}</td>
<td>{{$val['title']}}</td>
<!--这里的文章所属栏目list_id作为lists下标,对应的title就是文章所属的栏目名-->
<td>{{$lists[$val['list_id']]['title']}}</td>
</tr>
@endforeach
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!