laravel 5.4中實現(xiàn)無限級分類的方法示例
前言
本文主要給大家介紹的是關(guān)于laravel 5.4中實現(xiàn)無限級分類的相關(guān)內(nèi)容,分享出來供有需要的朋友們參考學習,下面話不多說,來一起看看詳細的介紹吧。
方法如下:
1、建立表
php artisan make:migration create_category_table --create=category
在database/migrations/下找到你的遷移文件
建入:
<?php use Illuminate\Support\Facades\Schema; use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Migrations\Migration; class CreateCategoryTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::create('categorys', function (Blueprint $table) { $table->increments('id'); $table->integer('parent_id'); $table->string('code'); $table->string('name'); $table->string('path'); $table->timestamps(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::dropIfExists('categorys'); } } php artisan migrate
2、建Model 在app/Category.php
php artisan make: model Category -m
<?php namespace App; use Illuminate\Database\Eloquent\Model; class Category extends Model { public function childCategory() { return $this->hasMany('App\Category', 'parent_id', 'id'); } public function allChildrenCategorys() { return $this->childCategory()->with('allChildrenCategorys'); } }
3、調(diào)用
$categorys = App/Category::with('allChildrenCategorys')->first();
或
$categorys->allChildrenCategorys;
或
$categorys->allChildrenCategorys->first()->allChildrenCategorys;
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學習或者使用laravel能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。
相關(guān)文章
PHP?ceil()函數(shù)浮點數(shù)向上取整實現(xiàn)示例
這篇文章主要為大家介紹了PHP?ceil()函數(shù)實現(xiàn)浮點數(shù)向上取整示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2024-01-01php把數(shù)據(jù)表導出為Excel表的最簡單、最快的方法(不用插件)
很多時候,數(shù)據(jù)庫中的數(shù)據(jù)需要導出成excel,以下是最簡便的方法,不用導出excel的類,即使功能簡單,但是對于沒有復雜需求的項目“見效快”2014-05-05Laravel中任務(wù)調(diào)度console使用方法小結(jié)
這篇文章主要給大家簡單介紹了Laravel中任務(wù)調(diào)度console使用方法,并附上一個簡單的示例,希望對大家學習使用console能夠有所幫助2017-05-05destoon調(diào)用discuz論壇中帶圖片帖子的實現(xiàn)方法
這篇文章主要介紹了destoon調(diào)用discuz論壇中帶圖片帖子的實現(xiàn)方法,是destoon開發(fā)中非常實用的一個技巧,需要的朋友可以參考下2014-08-08PHP將URL轉(zhuǎn)換成短網(wǎng)址的算法分享
短網(wǎng)址(Short URL)顧名思義就是在形式上比較短的網(wǎng)址。在Web 2.0的今天,不得不說這是一個潮流。目前已經(jīng)有許多類似服務(wù),借助短網(wǎng)址您可以用簡短的網(wǎng)址替代原來冗長的網(wǎng)址,讓使用者可以更容易的分享鏈接,下面來看看如何用PHP實現(xiàn)這個功能,有需要的朋友們可以參考。2016-09-09Laravel框架基于中間件實現(xiàn)禁止未登錄用戶訪問頁面功能示例
這篇文章主要介紹了Laravel框架基于中間件實現(xiàn)禁止未登錄用戶訪問頁面功能,結(jié)合實例形式分析了Laravel框架中間件生成、注冊及使用相關(guān)操作技巧,需要的朋友可以參考下2019-01-01Laravel學習教程之model validation的使用示例
這篇文章主要給大家介紹了關(guān)于Laravel學習教程之model validation使用的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面來一起看看吧。2017-10-10