php laravel eloquent 多级联动循环 laravel 视图输出 laravel 无限极分类 改怎么写循环呢
下面是 city 模型
namespace App\Model;
use Illuminate\Database\Eloquent\Model;
class City extends Model
{
protected $table ='cities';
public function parentCity()
{
return $this->belongsTo('App\Model\City', 'pid', 'id');
}
public function childrenCities()
{
return $this->hasMany('App\Model\City', 'pid', 'id');
}
}
/**
* 内容资源列表数据模型仓库
*/
public function index()
{
//$city = $this->city->all();
$cities = City::with('childrenCities')->get();
return $cities;
}
print 输出的数据如下 关联 子分类
$this->childrenCities
如果 有下级分类就会有
childrenCities
如果没有
childrenCities
返回
null
[{"id":19,"pid":0,"name":"\u4e0a\u6d77\u5e02","tag":"shs,shanghaishi","status":0,"created_at":"2015-07-07 09:29:04","updated_at":"2015-07-07 09:29:04","pname":"\u9876\u7ea7\u57ce\u5e02","children_cities":[{"id":20,"pid":19,"name":"\u666e\u9640\u533a","tag":"shs,shanghaishi","status":0,"created_at":"2015-07-07 09:29:04","updated_at":"2015-07-07 09:29:04"},{"id":21,"pid":19,"name":"\u5949\u8d24\u533a","tag":"shs,shanghaishi","status":0,"created_at":"2015-07-07 09:29:04","updated_at":"2015-07-07 09:29:04"},{"id":22,"pid":19,"name":"\u8679\u53e3\u533a","tag":"shs,shanghaishi","status":0,"created_at":"2015-07-07 09:29:04","updated_at":"2015-07-07 09:29:04"},{"id":23,"pid":19,"name":"\u5609\u5b9a\u533a","tag":"shs,shanghaishi","status":0,"created_at":"2015-07-07 09:29:04","updated_at":"2015-07-07 09:29:04"},{"id":24,"pid":19,"name":"\u9759\u5b89\u533a","tag":"shs,shanghaishi","status":0,"created_at":"2015-07-07 09:29:04","updated_at":"2015-07-07 09:29:04"},{"id":25,"pid":19,"name":"\u5362\u6e7e\u533a","tag":"shs,shanghaishi","status":0,"created_at":"2015-07-07 09:29:04","updated_at":"2015-07-07 09:29:04"},{"id":26,"pid":19,"name":"\u95f5\u884c\u533a","tag":"shs,shanghaishi","status":0,"created_at":"2015-07-07 09:29:04","updated_at":"2015-07-07 09:29:04"},{"id":27,"pid":19,"name":"\u6d66\u4e1c\u65b0\u533a","tag":"shs,shanghaishi","status":0,"created_at":"2015-07-07 09:29:04","updated_at":"2015-07-07 09:29:04"},{"id":28,"pid":19,"name":"\u5b9d\u5c71\u533a","tag":"shs,shanghaishi","status":0,"created_at":"2015-07-07 09:29:04","updated_at":"2015-07-07 09:29:04"},{"id":29,"pid":19,"name":"\u9752\u6d66\u533a","tag":"shs,shanghaishi","status":0,"created_at":"2015-07-07 09:29:04","updated_at":"2015-07-07 09:29:04"},{"id":30,"pid":19,"name":"\u677e\u6c5f\u533a","tag":"shs,shanghaishi","status":0,"created_at":"2015-07-07 09:29:04","updated_at":"2015-07-07 09:29:04"},{"id":31,"pid":19,"name":"\u5f90\u6c47\u533a","tag":"shs,shanghaishi","status":0,"created_at":"2015-07-07 09:29:04","updated_at":"2015-07-07 09:29:04"},{"id":32,"pid":19,"name":"\u6768\u6d66\u533a","tag":"shs,shanghaishi","status":0,"created_at":"2015-07-07 09:29:04","updated_at":"2015-07-07 09:29:04"},{"id":33,"pid":19,"name":"\u95f8\u5317\u533a","tag":"shs,shanghaishi","status":0,"created_at":"2015-07-07 09:29:04","updated_at":"2015-07-07 09:29:04"},{"id":34,"pid":19,"name":"\u957f\u5b81\u533a","tag":"shs,shanghaishi","status":0,"created_at":"2015-07-07 09:29:04","updated_at":"2015-07-07 09:29:04"},{"id":35,"pid":19,"name":"\u9ec4\u6d66\u533a","tag":"shs,shanghaishi","status":0,"created_at":"2015-07-07 09:29:04","updated_at":"2015-07-07 09:29:04"}]},{"id":20,"pid":19,"name":"\u666e\u9640\u533a","tag":"shs,shanghaishi","status":0,"created_at":"2015-07-07 09:29:04","updated_at":"2015-07-07 09:29:04","pname":"\u4e0a\u6d77\u5e02","children_cities":[]},{"id":21,"pid":19,"name":"\u5949\u8d24\u533a","tag":"shs,shanghaishi","status":0,"created_at":"2015-07-07 09:29:04","updated_at":"2015-07-07 09:29:04","pname":"\u4e0a\u6d77\u5e02","children_cities":[]},{"id":22,"pid":19,"name":"\u8679\u53e3\u533a","tag":"shs,shanghaishi","status":0,"created_at":"2015-07-07 09:29:04","updated_at":"2015-07-07 09:29:04","pname":"\u4e0a\u6d77\u5e02","children_cities":[]},{"id":23,"pid":19,"name":"\u5609\u5b9a\u533a","tag":"shs,shanghaishi","status":0,"created_at":"2015-07-07 09:29:04","updated_at":"2015-07-07 09:29:04","pname":"\u4e0a\u6d77\u5e02","children_cities":[]},{"id":24,"pid":19,"name":"\u9759\u5b89\u533a","tag":"shs,shanghaishi","status":0,"created_at":"2015-07-07 09:29:04","updated_at":"2015-07-07 09:29:04","pname":"\u4e0a\u6d77\u5e02","children_cities":[]},{"id":25,"pid":19,"name":"\u5362\u6e7e\u533a","tag":"shs,shanghaishi","status":0,"created_at":"2015-07-07 09:29:04","updated_at":"2015-07-07 09:29:04","pname":"\u4e0a\u6d77\u5e02","children_cities":[]},{"id":26,"pid":19,"name":"\u95f5\u884c\u533a","tag":"shs,shanghaishi","status":0,"created_at":"2015-07-07 09:29:04","updated_at":"2015-07-07 09:29:04","pname":"\u4e0a\u6d77\u5e02","children_cities":[]},{"id":27,"pid":19,"name":"\u6d66\u4e1c\u65b0\u533a","tag":"shs,shanghaishi","status":0,"created_at":"2015-07-07 09:29:04","updated_at":"2015-07-07 09:29:04","pname":"\u4e0a\u6d77\u5e02","children_cities":[]},{"id":28,"pid":19,"name":"\u5b9d\u5c71\u533a","tag":"shs,shanghaishi","status":0,"created_at":"2015-07-07 09:29:04","updated_at":"2015-07-07 09:29:04","pname":"\u4e0a\u6d77\u5e02","children_cities":[]},{"id":29,"pid":19,"name":"\u9752\u6d66\u533a","tag":"shs,shanghaishi","status":0,"created_at":"2015-07-07 09:29:04","updated_at":"2015-07-07 09:29:04","pname":"\u4e0a\u6d77\u5e02","children_cities":[]},{"id":30,"pid":19,"name":"\u677e\u6c5f\u533a","tag":"shs,shanghaishi","status":0,"created_at":"2015-07-07 09:29:04","updated_at":"2015-07-07 09:29:04","pname":"\u4e0a\u6d77\u5e02","children_cities":[]},{"id":31,"pid":19,"name":"\u5f90\u6c47\u533a","tag":"shs,shanghaishi","status":0,"created_at":"2015-07-07 09:29:04","updated_at":"2015-07-07 09:29:04","pname":"\u4e0a\u6d77\u5e02","children_cities":[]},{"id":32,"pid":19,"name":"\u6768\u6d66\u533a","tag":"shs,shanghaishi","status":0,"created_at":"2015-07-07 09:29:04","updated_at":"2015-07-07 09:29:04","pname":"\u4e0a\u6d77\u5e02","children_cities":[]},{"id":33,"pid":19,"name":"\u95f8\u5317\u533a","tag":"shs,shanghaishi","status":0,"created_at":"2015-07-07 09:29:04","updated_at":"2015-07-07 09:29:04","pname":"\u4e0a\u6d77\u5e02","children_cities":[]},{"id":34,"pid":19,"name":"\u957f\u5b81\u533a","tag":"shs,shanghaishi","status":0,"created_at":"2015-07-07 09:29:04","updated_at":"2015-07-07 09:29:04","pname":"\u4e0a\u6d77\u5e02","children_cities":[]},{"id":35,"pid":19,"name":"\u9ec4\u6d66\u533a","tag":"shs,shanghaishi","status":0,"created_at":"2015-07-07 09:29:04","updated_at":"2015-07-07 09:29:04","pname":"\u4e0a\u6d77\u5e02","children_cities":[]},{"id":36,"pid":0,"name":"\u6c5f\u82cf\u7701","tag":"","status":0,"created_at":"2015-07-27 11:55:55","updated_at":"2015-07-27 11:55:55","pname":"\u9876\u7ea7\u57ce\u5e02","children_cities":[{"id":37,"pid":36,"name":"\u5357\u4eac\u5e02","tag":"","status":0,"created_at":"2015-07-27 11:57:48","updated_at":"2015-07-27 11:57:48"}]},{"id":37,"pid":36,"name":"\u5357\u4eac\u5e02","tag":"","status":0,"created_at":"2015-07-27 11:57:48","updated_at":"2015-07-27 11:57:48","pname":"\u6c5f\u82cf\u7701","children_cities":[{"id":38,"pid":37,"name":"\u7ebd\u533a","tag":"","status":0,"created_at":"2015-07-27 11:58:41","updated_at":"2015-07-27 11:58:41"}]},{"id":38,"pid":37,"name":"\u7ebd\u533a","tag":"","status":0,"created_at":"2015-07-27 11:58:41","updated_at":"2015-07-27 11:58:41","pname":"\u5357\u4eac\u5e02","children_cities":[]}]
/**
* view - 视图模板 循环输出2级
*/
@foreach($cities as $key => $li)
@if($li->pid == 0)
<tr>
<td class="text-aqua">{{ $li -> name }}</td>
<td class="text-green">{{ $li -> tag }}</td>
<td> <a href="{{ route('admin.city.index') }}/{{ $li->id }}/edit"><i class="fa fa-fw fa-pencil" title="修改"></i></a> </td>
</tr>
@foreach($li->childrenCities as $li2)
<tr>
<td class="text-aqua"> -- {{ $li2 -> name }}</td>
<td class="text-green">{{ $li2 -> tag }}</td>
<td> <a href="{{ route('admin.city.index') }}/{{ $li2->id }}/edit"><i class="fa fa-fw fa-pencil" title="修改"></i></a> </td>
</tr>
@endforeach
@endif
@endforeach
目前是这么写的,感觉不应该这么写 输出 ,按照三级栏目联动循环的数据展现;
抛弃 城市的 上万条数据不讲;
现在就是想 输出三级栏目分类; 城市只是举个列子;
三级栏目改怎么在视图 输出呢? 反复用 foreach 循环吗
还是有其他 方案输出呢?
小白,迷茫了啊 ;
有大神指导一下吗 ; 怎么在 视图 输出呢
现在输出 如下 样式图
筷子要5角
9 years, 4 months ago