PHP Laravel Eloquent Multi-Level Linkage Loop Laravel View Output Laravel Infinite Classification So schreiben Sie eine Schleife
PHP中文网
PHP中文网 2017-05-16 16:55:30
0
3
970

Das Folgende ist das Stadtmodell

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;

}
Die von

print ausgegebenen Daten lauten wie folgt: Assoziationsunterkategorie $this->childrenCities

Wenn es Kategorien auf niedrigerer Ebene gibt, wird es childrenCities 如果没有 childrenCities 返回 null

geben

[{"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

So wird es derzeit geschrieben, aber ich bin der Meinung, dass es nicht so geschrieben werden sollte. Die Ausgabe wird gemäß den Daten des dreistufigen Spaltenverknüpfungszyklus angezeigt

Gib die Zehntausenden von Daten der Stadt auf

Jetzt möchte ich nur noch die dreistufige Spaltenklassifizierung ausgeben; die Stadt ist nur ein Beispiel;

Wie wird die Spalte der dritten Ebene in der Ansicht ausgegeben? Verwenden Sie die foreach-Schleife wiederholt?

Gibt es noch andere Lösungen für die Ausgabe?

Xiaobai, ich bin verwirrt;

Kann mir jemand eine Anleitung geben, wie man in View ausgibt?

Geben Sie nun das folgende Stildiagramm aus

PHP中文网
PHP中文网

认证0级讲师

Antworte allen(3)
曾经蜡笔没有小新

你需要用递归实现。

我想大声告诉你

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

我想大声告诉你

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

Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage