使用Laravel获取父子数据

时间:2018-12-03 17:12:36

标签: php mysql laravel-5 mysqli relationship

我正在使用Laravel 5.7,我想使用child显示parent id.数据

我创建了一个CategoryController,并且通过此处显示的parent id来获取数据。但是,我想显示index页中的所有数据。

<?php

$parentCat = Category::where('parent', 0)->get();
foreach ($parentCat as $parent) {
    echo "<pre>";
    echo $parent->category_name;
    $category = Category::where('parent', $parent->id)->get();

    foreach ($category as $child) {
        echo "<pre>";
        echo $child->category_name;
    }

}

return view('fnf.category.index')->with('parent', $parentCat);

enter image description here

<table class="table table-striped">
    <tr>
        <th>Man</th>
        <th><a href=""> Edit</a></th>
    </tr>
    <tr>
        <td>Shirt</td>
        <td><a href=""> Edit</a></td>
    </tr>
    <tr>
        <td>Shirt</td>
        <td><a href=""> Edit</a></td>
    </tr>
    <tr>
        <th>Woman</th>
        <th><a href=""> Edit</a></th>
    </tr>
    <tr>
        <td>Woman Shirt</td>
        <td><a href=""> Edit</a></td>
    </tr>
</table>

enter image description here

0 个答案:

没有答案