php laravel eloquent multi-level linkage loop laravel view output laravel infinite classification how to write loop
PHP中文网
PHP中文网 2017-05-16 16:55:30
0
3
977

The following is the city model

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 The output data is as follows Association subcategory $this->childrenCities

If there is a lower-level category, there will be childrenCities If there is no childrenCities, return 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

This is currently written like this, but I feel that it should not be written like this. Output is displayed according to the data display of the three-level column linkage cycle;

Abandon the city’s tens of thousands of data;

Now I just want to output the three-level column classification; the city is just an example;

How to output the third-level column in the view? Do you use foreach loop repeatedly?

Are there any other solutions for output?

Xiaobai, I’m confused;

Does anyone have any guidance? How to output in view

Now the output is as follows style diagram

PHP中文网
PHP中文网

认证0级讲师

reply all(3)
曾经蜡笔没有小新

You need to implement it recursively.

我想大声告诉你

I am against recursive implementation. Solving it from the perspective of database design can obviously solve the program burden.
Relational row database stores multi-level data. It is best not to simply do pid. id. Such.
Or add the path of each layer. Or change the structure. If you are a math master, you can also use matrices to calculate directly.

我想大声告诉你

Why there is no answer? Has the poster solved it~~ ^-^

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template