Implementation of infinite level classification for laravel

Time:2020-11-4

Original address:https://www.wjcms.net/archives/laravel Implementation of infinite level classification

The words written in the front

Infinite classification is basically involved in all websites, so it is a knowledge point that must be mastered. When reading a lot of materials and documents on the Internet, it is either not meticulous, or it is not at all right, or it can not achieve the expected goal. In fact, the idea and method of realizing it are very simple. Let’s realize it together today.

Create model controller data migration file

This is created directly using the artisan command

#- A is in fact all, creating the containing model, controller (resource), data migration file (factory model, seed)
			php artisan make:model -a Category

Run this command to create the resource controller.

Modify data migration file

First, modify the data migration file XXX_ create_ categories_ table.

Open the file, modify the up method in it, and add the corresponding fields.

Schema::create('categories', function (Blueprint $table) {
			$table->id();
			$table - > string ('title ', 100) - > comment ('category name');
			$table - > string ('name ', 100) - > comment ('classification identification');
			$table - > string ('description ', 255) - > nullable() - > comment ('classification description');
			$Table > integer ('pid ') - > Default (0) - > comment ('classification ID');
			$table - > integer ('level ') - > Default (1) - > comment ('classification level');
			$Table > integer ('sort ') - > Default (0) - > comment ('sort');
			$table - > integer ('status') - > Default (1) - > comment ('status: 0-disabled, 1-normal ');
			$table->timestamps();
			});

Execute migration command

php artisan migrate

Nested model implementation read

//App\Models\Category.php

			public function categories()
			{
			return $this->hasMany(self::class, 'pid', 'id')->with('categories');
			}

Controller call

//app\Http\controllers\CategooryController.php
			#Use model
			use App\Models\Category;

			public function index()
			{
			$categories = Category::with('categories')->where('pid', 0)->get();
			return view('category.index', compact('categories'));
			}

Add route

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

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

Blade template rendering

Recursive rendering is used here.

In resources / views/ categories.blade.php Document:

number
						Classification name
						Classification identification
						Classification description
						Creation time
						state
						operation
					
				
				
					@foreach ($categories as $category)
					
						{{ $category->id }}
						{{ $category->title }}
						
							{{ $category->name }}
						
						{{ $category->description }}
						{{ $category->created_at }}
						
							{{ $category->status }}
						
						
					
					
					@foreach ($category->categories as $childCategory)
					@include('category.child_category', ['child_category' => $childCategory])
					@endforeach
					@endforeach

Recursive part loads its own template child_ category.blade.php

{{ $child_category->id }}
				|{{ str_repeat('--',$child_category->level-1) }} {{ $child_category->title }}
				
					{{ $child_category->name }}
				
				{{ $child_category->description }}
				{{ $child_category->created_at }}
				
					{{ $child_category->status }}
				
				
			
			
			@if ($child_category->categories)
			@foreach ($child_category->categories as $childCategory)
			@include('category.child_category', ['child_category' => $childCategory])
			@endforeach
			@endif

Finally, let’s take a look at the effect