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;
}
Les données générées par print sont les suivantes : Sous-catégorie Association $this->childrenCities
S'il y a des catégories de niveau inférieur, il y en aura 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"> -- {{ $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
C'est ainsi que cela est actuellement écrit. Je pense que cela ne devrait pas être écrit comme ça. La sortie est affichée en fonction des données du cycle de liaison de colonnes à trois niveaux ;
Abandonnez les dizaines de milliers de données de la villeMaintenant, je veux juste afficher la classification des colonnes à trois niveaux ; la ville n'est qu'un exemple ;
Comment afficher la colonne de troisième niveau dans la vue ? Utilisez-vous la boucle foreach à plusieurs reprises ?
Existe-t-il d'autres solutions pour la sortie ?
Xiaobai, je suis confus ;
Quelqu'un peut-il me donner des conseils ? Comment afficher en vue
Produisez maintenant le diagramme de style suivant
Vous devez l'implémenter de manière récursive.
Je suis contre l'implémentation récursive. Le résoudre du point de vue de la conception de la base de données peut évidemment résoudre le fardeau du programme.
La base de données de lignes relationnelles stocke des données à plusieurs niveaux. Il est préférable de ne pas simplement faire du pid. identifiant. Comme ça.
Ou ajoutez le chemin de chaque calque. Ou changer la structure. Si vous maîtrisez les mathématiques, vous pouvez également utiliser des matrices pour calculer directement.
Pourquoi il n'y a pas de réponse ? L'affiche a-t-elle résolu le problème~~ ^-^