Use hasMany of Laravel Eloquent to develop infinite classification

Keywords: PHP Laravel Database


In the online shopping mall, we can often see multi-level classification, sub classification and even unlimited classification. This article will show you how to implement it gracefully through Laravel Eloquent.


We will create a micro project to show the classification of children's shops, with 5 levels in total, as follows:

Database migration

Simple data table structure:

Schema::create('categories', function (Blueprint $table) {

There is only one name field associated with itself. Therefore, most of the parent categories have a parent id = null

The data in the data table are as follows:

Eloquent model and correlation

First, create a simple hasMany() method in app/Category.php. The classification may have its own classification:

class Category extends Model
 public function categories()
 return $this->hasMany(Category::class);


The best "strategy" in the beginning of a good play. Did you know that recursive relationships can be described like this? As follows:

public function childrenCategories()
 return $this->hasMany(Category::class)->with('categories');


Therefore, if you call Category::with('categories'), you will get lower level "subcategories", but you can achieve unlimited through Category::with('childrenCategories').

Routing and controller methods

Now, let's try to show all categories and subcategories, as shown in the example above.

In routes/web.php, we add the following:

Route::get('categories', 'CategoryController@index');


app/Http/CategoryController.php is as follows:

public function index()
 $categories = Category::whereNull('category_id')
 return view('categories', compact('categories'));


We load only the parent category, with the child category as the relationship. Easy, right?

Views and recursive subviews

Finally, render to the page. In the resources/views/categories.blade.php file:

 @foreach ($categories as $category)
 <li>{{ $category->name }}</li>
 @foreach ($category->childrenCategories as $childCategory)
 @include('child_category', ['child_category' => $childCategory])


We first traverse the top-level parent category, then traverse the child categories of the parent category, and then use @ include to load the child categories of the child categories

The best part is that resources/views/admin/child_category.blade.php will use recursion to load itself. See code:

<li>{{ $child_category->name }}</li>
@if ($child_category->categories)
 @foreach ($child_category->categories as $childCategory)
 @include('child_category', ['child_category' => $childCategory])


In child category.blade.php, we include @ include('child category '), so as long as there are categories in the current subcategory, the template will recursively load subcategories.

this is it! We have infinite levels of subcategories - whether in databases, relationships, or views

For more information, please visit:

Tencent T3-T4 standard boutique PHP architect tutorial directory, as long as you read it to ensure a higher salary (continuous update)

Posted by nitram on Mon, 11 May 2020 00:03:54 -0700