Laravel查询或嵌套/无限子类别的函数

时间:2016-03-09 06:30:41

标签: php mysql laravel

类别表

ID | parent_id |名称|
    1 | 0 |人力资源|
    2 | 1 |设置|
    3 | 2 |部门|
    4 | 3 |位置|

我想这样:

Array
(
    [0] => stdClass Object
        (
            [id] => 1
            [parent_id] => 0
            [name] => Human Resource
            [created_at] => 0000-00-00 00:00:00
            [updated_at] => 2016-03-08 05:43:09
            [sub_category] => Array
                (
                    [0] => stdClass Object
                        (
                            [id] => 4
                            [parent_id] => 1
                            [page_name] => Settings
                            [icon] => 
                            [created_at] => 2016-03-09 11:53:35
                            [updated_at] => 2016-03-08 05:52:34
                        )

                )

        )

)
  

注意:我想要一个动态/无限子类别。没有限制   子类别的级别。

我已经有这样的查询:

$ categories = DB :: table('category') - > where('parent_id',0) - > get();
foreach($ categories as $ key => $ value){
$ value-> sub_category = DB :: table('category') - > where('parent_id',$ value-> id) - > get();
}

但是我怎么能做这个递归循环呢?因为我希望拥有无限级别的子类别。
防爆。
>父猫
>父猫
>子猫
>第二子猫
>第三个子猫等等...

2 个答案:

答案 0 :(得分:1)

你可以用它 的控制器

public function create()
{
    $categories = Category::all();
    return view('backend.categories.create')->with('categories', $categories);
}

<强>模型

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Category extends Model
{
    protected $guarded = ['id'];

    public function parent()
    {
        return $this->belongsTo('App\Category', 'parent_id');
    }

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

查看

<table class="table table-bordered table-hover">
                <thead>
                    <tr>
                        <th>Name</th>
                        <th>Description</th>
                        <th>Slug</th>
                        <th>Action</th>
                    </tr>
                </thead>
                <tbody>
                @foreach ($categories as $category)
                    <tr>
                        <td>{{ $category->name }}</td>
                        <td>{{ $category->description }}</td>
                        <td>{{ $category->slug }}</td>
                        <td><a class="edit" href="{!! action('Admin\CategoriesController@edit', $category->id) !!}" title="Edit"><i class="fa fa-pencil-square-o"></a></i> <a class="delete" href="{!! action('Admin\CategoriesController@destroy', $category->id) !!}" title="Are you sure you want to delete?"><i class="fa fa-trash-o"></i></a></td>
                        @foreach ($category->children as $children)
                            <tr>
                                <td>{{ $children->name }}</td>
                                <td>{{ $children->description }}</td>
                                <td>{{ $children->slug }}</td>
                                <td></td>
                            </tr>
                        @endforeach
                    </tr>
                </tbody>

                @endforeach
            </table>

如果已经运行了循环,则让子项循环使用条件

答案 1 :(得分:0)

你可以使用它。

public function view() {
$categories = Category::with('children')->get();

return compact('categories');  // or use json_encode
}