php artisan make:migration create_category_table --create=category
在database/migrations/
下找到你的遷移文件
建入:php
<?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
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'); } }
$categorys = App/Category::with('allChildrenCategorys')->first();
或數組
$categorys->allChildrenCategorys;
或app
$categorys->allChildrenCategorys->first()->allChildrenCategorys;
有朋友問到這個問題我就更新到這了this
$arr = []; array_walk_recursive($categories,function ($v, $k) use(&$arr) { if($k == 'id') $arr[] = $v; });