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">  &nbsp;&nbsp; -- {{ $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 循环吗

还是有其他 方案输出呢?

小白,迷茫了啊 ;

有大神指导一下吗 ; 怎么在 视图 输出呢

现在输出 如下 样式图

图片描述

阅读 12.8k
6 个回答

你需要用递归实现。

我反对递归实现。从数据库设计的角度去解决可以明显解决程序负担。
关系行数据库在存储多级数据的时候。最好不要单纯做pid。id。这样的。
要么加上每一层的路径。要么换结构。要是数学大神还可以用矩阵来直接计算。

新手上路,请多包涵

咋没有回答的呢,楼主有没有解决啊~~ ^-^

新手上路,请多包涵

供参考

public function children()
{
    return $this->hasMany(Category::class, 'parent_id');
}

/**
*  获取子分类
*/
public function getChild($payload = null) {
    $payload = $payload ? $payload : [$this->id];
    $this->children->each(function($child) use (&$payload){
        $payload = array_merge([$child->id], $child->getChild($payload));
    });
    return $payload;
}
    
// 使用
$categoryIds = Category::find($request->category_id)->getChild();
$Book = $Book->whereIn('category_id', $categoryIds);

我今天也在搞一个递归显示的问题。

1. 在模型里面

public function childMessages(){
    return $this->hasMany('App\Message','replyId','id'); //->with('childMessages')
}

2. 控制器里面的方法

    use Illuminate\Database\Eloquent\Collection;
    
    /**
     * 递归获取数据
     * @param Collection $a, Collection $b
     * @return null
     */
    public function childM(Collection $a,Collection &$b)
    {
        foreach($a as &$v){
            if(count($v->childMessages)>0){
                $b->add($v);
                $this->childM($v->childMessages,$b);
            }else{
                $b->add($v);
            }
        }
    }

3. 控制器调用

$all=Message::with('childMessages')->where('replyId','0')->get();

$all2=(new Collection);
$this->childM($all,$all2);
 
return view('messages/index',[
    'messages'=>$all2
]);

4. 之后视图输出就可以了


p.s. 在模型里面如果直接return $this->hasMany('App\Message','replyId','id')->with('childMessages');
然后$all=Message::with('childMessages')->where('replyId','0')->get();调用,结果里面会直接返回所有递归,但是这里不用管。因为在递归中使用$v->childMessages时,会自己再次查询。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
宣传栏