laravel 5.4中實(shí)現(xiàn)無(wú)限級(jí)分類的方法示例
前言
本文主要給大家介紹的是關(guān)于laravel 5.4中實(shí)現(xiàn)無(wú)限級(jí)分類的相關(guān)內(nèi)容,分享出來(lái)供有需要的朋友們參考學(xué)習(xí),下面話不多說(shuō),來(lái)一起看看詳細(xì)的介紹吧。
方法如下:
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é)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者使用laravel能帶來(lái)一定的幫助,如果有疑問(wèn)大家可以留言交流,謝謝大家對(duì)腳本之家的支持。
相關(guān)文章
PHP?ceil()函數(shù)浮點(diǎn)數(shù)向上取整實(shí)現(xiàn)示例
這篇文章主要為大家介紹了PHP?ceil()函數(shù)實(shí)現(xiàn)浮點(diǎn)數(shù)向上取整示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2024-01-01php把數(shù)據(jù)表導(dǎo)出為Excel表的最簡(jiǎn)單、最快的方法(不用插件)
很多時(shí)候,數(shù)據(jù)庫(kù)中的數(shù)據(jù)需要導(dǎo)出成excel,以下是最簡(jiǎn)便的方法,不用導(dǎo)出excel的類,即使功能簡(jiǎn)單,但是對(duì)于沒(méi)有復(fù)雜需求的項(xiàng)目“見(jiàn)效快”2014-05-05Laravel中任務(wù)調(diào)度console使用方法小結(jié)
這篇文章主要給大家簡(jiǎn)單介紹了Laravel中任務(wù)調(diào)度console使用方法,并附上一個(gè)簡(jiǎn)單的示例,希望對(duì)大家學(xué)習(xí)使用console能夠有所幫助2017-05-05destoon調(diào)用discuz論壇中帶圖片帖子的實(shí)現(xiàn)方法
這篇文章主要介紹了destoon調(diào)用discuz論壇中帶圖片帖子的實(shí)現(xiàn)方法,是destoon開(kāi)發(fā)中非常實(shí)用的一個(gè)技巧,需要的朋友可以參考下2014-08-08php is_writable判斷文件是否可寫(xiě)實(shí)例代碼
這篇文章主要介紹了php is_writable判斷文件是否可寫(xiě)實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下2016-10-10PHP將URL轉(zhuǎn)換成短網(wǎng)址的算法分享
短網(wǎng)址(Short URL)顧名思義就是在形式上比較短的網(wǎng)址。在Web 2.0的今天,不得不說(shuō)這是一個(gè)潮流。目前已經(jīng)有許多類似服務(wù),借助短網(wǎng)址您可以用簡(jiǎn)短的網(wǎng)址替代原來(lái)冗長(zhǎng)的網(wǎng)址,讓使用者可以更容易的分享鏈接,下面來(lái)看看如何用PHP實(shí)現(xiàn)這個(gè)功能,有需要的朋友們可以參考。2016-09-09Laravel框架基于中間件實(shí)現(xiàn)禁止未登錄用戶訪問(wèn)頁(yè)面功能示例
這篇文章主要介紹了Laravel框架基于中間件實(shí)現(xiàn)禁止未登錄用戶訪問(wèn)頁(yè)面功能,結(jié)合實(shí)例形式分析了Laravel框架中間件生成、注冊(cè)及使用相關(guān)操作技巧,需要的朋友可以參考下2019-01-01Laravel學(xué)習(xí)教程之model validation的使用示例
這篇文章主要給大家介紹了關(guān)于Laravel學(xué)習(xí)教程之model validation使用的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起看看吧。2017-10-10