Laravel 使用 Laravel-nestedset 实现无限分类 
                            
                                                    
                        
                    
                    
  
                    
                    安装nestedset
composer require kalnoy/nestedset数据库迁移具体说明请参考:github.com/lazychaser/laravel-nest...
具体迁移代码:
public function up()
    {
        Schema::create('subjects', function (Blueprint $table) {
            $table->increments('id')->comment('科目ID');
            $table->string('name')->notNULL()->comment('科目名称');
            $table->nestedSet();
            $table->string('intro')->nullable()->comment('科目简介');
        });
    }迁移后数据库结构,可以看到多了三个字段
模型 引入需要的文件
use Kalnoy\Nestedset\NodeTrait;
class Foo extends Model {
    use NodeTrait;
}控制器
<?php
namespace App\Http\Controllers\Admin;
use App\Admin\Subject;
use App\Http\Controllers\Controller;
use App\Http\Requests\SubjectRequest;
use App\Repositories\SubjectRepositories;
use Illuminate\Http\Request;
class SubjectController extends Controller
{
    protected $repo;
    public function __construct(SubjectRepositories $repo)
    {
        $this->repo = $repo;
    }
//用来展示分类的方法index
    public function index()
    {
        $data = Subject::with('children')->withDepth()->get()->toFlatTree();
        return view('admin.subject', compact('data'));
    }
//用来增加分类的方法add
    public function add(SubjectRequest $request)
    {
        return $this->repo->save($request) ? '1' : '0';
    }
}
add方法里面的save代码
public function save($request)
    {
        $data = $request->only(['name', 'parent_id']);
        $subject = Subject::create($data);
        if ($request->parent_id) {
            $node = Subject::find($request->parent_id);
            $node->appendNode($subject);
        }
        return $subject;
    }视图文件 admin.subject
                                <tbody class="x-cate">
                                @foreach($data as $val)
                                <tr cate-id='{{$val->id}}' fid='{{$val->parent_id ?? '0'}}' >
                                    <td>
                                        <input type="checkbox" name="" lay-skin="primary">
                                    </td>
                                    <td>{{$val->id}}</td>
                                    @if($val->children->isEmpty() and $val->parent_id)
                                    <td>{!! str_repeat("        ",$val->depth) !!}├ {{$val->name}}
                                    </td>@elseif($val->children->isEmpty() and $val->parent_id==0)
                                        <td>{{$val->name}}</td>@else
                                    <td>{!! str_repeat("       ",$val->depth) !!}<i class="layui-icon x-show" status='true'></i>{{$val->name}}</td>@endif
                                </tr>
                                 @endforeach
                                </tbody>效果展示

 
           
         
             
             
             
                     
                     
             
             
           
           关于 LearnKu
                关于 LearnKu
               
                     
                     
                     粤公网安备 44030502004330号
 粤公网安备 44030502004330号 
 
推荐文章: