我們會創(chuàng)建一個微型項目來展示兒童商店的分類,總共有 5 級,如下:
簡單的數(shù)據(jù)表結(jié)構(gòu):
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(); });
只有一個 name 字段, 關(guān)聯(lián)到其自身。所以,大部分父級分類 category_id = NULL,每一個子分類都有一個 parent_id
數(shù)據(jù)表數(shù)據(jù)如下:
首先,在 app/Category.php 創(chuàng)建一個簡單的 hasMany() 方法, 分類可能擁有其自分類:
class Category extends Model { public function categories() { return $this->hasMany(Category::class); } }
好戲開場 本文最妙 “計策”。你知道可以向這樣描述 遞歸 關(guān)系嗎?如下:
public function childrenCategories() { return $this->hasMany(Category::class)->with('categories'); }
因此,如果調(diào)用 Category::with(‘categories'),將得到下級 “子分類”,但是通過 Category::with(‘childrenCategories') 將能幫你實現(xiàn)無限極。
現(xiàn)在,讓我們嘗試顯示所有類別和子類別,如上例所示。
在 routes/web.php,我們添加以下內(nèi)容:
Route::get('categories', 'CategoryController@index');
app/Http/CategoryController.php 如下所示:
public function index() { $categories = Category::whereNull('category_id') ->with('childrenCategories') ->get(); return view('categories', compact('categories')); }
我們僅加載父類別,將子類別作為關(guān)系。簡單吧?
最后,渲染到頁面。 在 resources/views/categories.blade.php 文件:
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>
我們先遍歷了最頂級的父類別,然后遍歷出父類的子類別,然后使用 @include 加載子類別的子類別......
最好的部分是 resources/views/admin/child_category.blade.php 將使用遞歸加載自身??创a:
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
在 child_category.blade.php 內(nèi)部,我們包含了 @include(‘child_category'),因此只要當前子類別中有類別,模板就會遞歸地加載子類別。
就是這樣!我們擁有無限級別的子類別 - 無論是在數(shù)據(jù)庫還是關(guān)聯(lián)關(guān)系或是視圖中
以上就是如何使用Laravel Eloquent來開發(fā)無限極分類的詳細內(nèi)容,更多關(guān)于使用Laravel Eloquent來開發(fā)無限極分類的資料請關(guān)注腳本之家其它相關(guān)文章!