Skip to content

Instantly share code, notes, and snippets.

@t301000
Last active August 29, 2015 14:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save t301000/047baadc56f2e1c095f4 to your computer and use it in GitHub Desktop.
Save t301000/047baadc56f2e1c095f4 to your computer and use it in GitHub Desktop.
無窮分類列表範例
<?php
use Illuminate\Database\Seeder;
class CategoriesTableSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
App\Category::create([
'of_id' => 0,
'title' => '羽球'
]);
App\Category::create([
'of_id' => 0,
'title' => '棒球'
]);
App\Category::create([
'of_id' => 0,
'title' => '籃球'
]);
App\Category::create([
'of_id' => 1,
'title' => '羽球袋'
]);
App\Category::create([
'of_id' => 1,
'title' => '羽球鞋'
]);
App\Category::create([
'of_id' => 3,
'title' => '籃球衣'
]);
App\Category::create([
'of_id' => 3,
'title' => '籃球鞋'
]);
App\Category::create([
'of_id' => 1,
'title' => '羽球拍'
]);
App\Category::create([
'of_id' => 5,
'title' => '美津濃'
]);
App\Category::create([
'of_id' => 5,
'title' => 'nike'
]);
}
}
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Category extends Model
{
}
<?php
namespace App\Http\Controllers;
use App\Category;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
class CategoryController extends Controller {
/**
* Display a listing of the resource.
*
* @return Response
*/
public function index()
{
$categories = $this->getChildCategories();
return view( 'categories.index', compact('categories') );
}
/**
* 由最上層開始遞迴取得各層分類
* @param int $of_id 上層分類id,0 為最上層
* @return array
*/
private function getChildCategories( $of_id = 0 )
{
$item = [];
// 取得某一個分類的第一層子分類,並且只取回 id of_id title 欄位
// 第一次取得的是最上層
// $categories 為 collection
$categories = Category::where( 'of_id', $of_id )
->get( [ 'id', 'of_id', 'title' ] );
// 遞迴取得所有下層子分類
foreach ( $categories as $category ) {
$childs = $this->getChildCategories( $category[ 'id' ] );
// 某分類及其子分類包成陣列後存入陣列
$item[] = compact( 'category', 'childs' );
}
return $item;
}
}
<div>
{{ str_repeat('----', isset($i) ? $i++ : 0) . $category['category']->title }} <br/>
@if(count($category['childs'])>0)
@foreach($category['childs'] as $category)
@include('categories.child', ['category' => $category, 'i' => isset($i) ? $i : 1])
@endforeach
@endif
</div>
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateCategoriesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('categories', function (Blueprint $table) {
$table->increments('id');
$table->integer('of_id');
$table->string('title');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('categories');
}
}
@each('categories.child', $categories, 'category')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment