How to use laravel eloquent to develop infinite classification

Time:2022-6-10
catalogue
  • summary
  • Database migration
  • Eloquent model and correlation
  • Routing and controller methods
  • Views and recursive subviews

summary

We will create a mini project to show the classification of children’s stores, with a total of 5 levels, as follows:

Database migration

Simple data table structure:


Schema::create('categories', function (Blueprint $table) {
 $table->bigIncrements('id');
 $table->string('name');
 $table->unsignedBigInteger('category_id')->nullable();
 $table->foreign('category_id')->references('id')->on('categories');
 $table->timestamps();
});

There is only one name field associated with itself. Therefore, most parent categories_ Id = null, each subcategory has a parent_ id

The data in the data sheet are as follows:

Eloquent model and correlation

First, in app/category PHP creates a simple hasmany () method, and the classification may have its own self classification:


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

This article is the best “trick” at the beginning of a good play. Did you know that recursion can be described in this way? As follows:


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

Therefore, if you call category:: with (‘categories’), you will get a lower level “sub category”, but category:: with (‘childrencategories’) will help you achieve infinity.

Routing and controller methods

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

At routes/web PHP, we add the following:


Route::get('categories', '[email protected]');

app/Http/CategoryController. PHP is as follows:


public function index()
{
 $categories = Category::whereNull('category_id')
 ->with('childrenCategories')
 ->get();
 return view('categories', compact('categories'));
}

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

Views and recursive subviews

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


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

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

The best part is resources/views/admin/child_ category. blade. PHP will use recursion to load itself. Look at the code:


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

At child_ category. blade. Inside PHP, we include @include (‘child_category’). Therefore, 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, associations, or views

The above is the details of how to use laravel eloquent to develop limitless classification. For more information about using laravel eloquent to develop limitless classification, please pay attention to other related developeppaer articles!