laravel集成baum

2024-03-12 20:32
文章标签 集成 laravel baum

本文主要是介绍laravel集成baum,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

安装git https://github.com/etrepat/baum 

运行install后会生成migrate,默认有id, parent_id, lft, rgt, depth, 其他根据自己需要自定

$table->increments('id');
$table->string('title')->comment('栏目名称');
$table->integer('parent_id')->nullable()->index()->comment('父级id'); //生成的
$table->string('keywords')->nullable();
$table->string('description')->nullable();
$table->integer('sort');
$table->integer('is_show')->comment('是否显示');
$table->integer('lft')->nullable()->index(); //生成的
$table->integer('rgt')->nullable()->index(); //生成的
$table->integer('depth')->nullable(); //生成的
模型:

model不继承Model,需要继承Node

<?phpnamespace App;use Illuminate\Database\Eloquent\Model;
use Baum;class Taxon extends  Baum\Node
{protected $table = 'taxons';protected $fillable=['title','parent_id','keywords','description', 'sort', 'is_show', 'lft', 'rgt', 'depth'];
}

自动填充

<?phpnamespace App\Jobs;use App\Taxon;
use Illuminate\Bus\Queueable;
use Illuminate\Foundation\Bus\Dispatchable;class TaxonFormFields
{use Dispatchable, Queueable;protected $id;protected $fieldList = ['title' => '','parent_id' => '','keywords' => '','description' => '','is_show' => '1','sort' => '50',];/*** Create a new job instance.** @return void*/public function __construct($id = null){$this->id = $id;}/*** Execute the job.** @return void*/public function handle(){$fields = $this->fieldList;if ($this->id) {$fields = Taxon::findOrFail($this->id);}return $fields;}
}


自定义request

<?phpnamespace App\Http\Requests;use Illuminate\Foundation\Http\FormRequest;class TaxonRequest extends FormRequest
{/*** Determine if the user is authorized to make this request.** @return bool*/public function authorize(){return true;}/*** Get the validation rules that apply to the request.** @return array*/public function rules(){return ['title' => 'required'];}/*** Return the fields and values to create a new post from*/public function fillData(){return ['title' => $this->title,'parent_id' => $this->parent_id,'keywords' => $this->keywords,'description' => $this->description,'is_show' => $this->is_show,'sort' => $this->sort,'lang' => 'zh-cn',];}
}


表单

<div class="form-group"><label for="taxon_title" class="col-sm-1 control-label">{{ trans('admin.title') }}</label><div class="col-sm-11"><input type="text" class="form-control" id="taxon_title" name="title" value="{{ $title }}"><p class="help-block">{{ $errors->first('title') }}</p></div>
</div>
<div class="form-group"><label for="taxon_keywords" class="col-sm-1 control-label">{{ trans('admin.keywords') }}</label><div class="col-sm-11"><input type="text" class="form-control" id="taxon_keywords" name="keywords" value="{{ $keywords }}"></div>
</div>
<div class="form-group"><label for="taxon_description" class="col-sm-1 control-label">{{ trans('admin.description') }}</label><div class="col-sm-11"><textarea class="form-control" id="taxon_description" name="description">{{ $description }}</textarea></div>
</div>
<div class="form-group"><label for="taxon_is_showe" class="col-sm-1 control-label">{{ trans('admin.is_show') }}</label><div class="col-sm-11"><input type="radio" id="taxon_is_show_1" name="is_show" value="{{ $is_show }}" @if($is_show == 1) checked="checked" @endif> {{ trans('admin.yes') }}<input type="radio" id="taxon_is_show_0" name="is_show" value="{{ $is_show }}" @if($is_show == 0) checked="checked" @endif> {{ trans('admin.no') }}</div>
</div>
<div class="form-group"><label for="taxon_sort" class="col-sm-1 control-label">{{ trans('admin.sort') }}</label><div class="col-sm-11"><input type="text" class="form-control" id="taxon_sort" name="sort" value="{{ $sort }}"></div>
</div>
<div class="form-group"><div class="col-sm-offset-1 col-sm-11"><input type="hidden" name="parent_id" value="{{ $parent_id }}" /><input type="submit" class="btn btn-default" value="提交" /></div>
</div>


添加分类

/*** 创建新表单页面** @return Response*/public function create(Request $request){$taxon = $this->dispatch(new TaxonFormFields());$parent_id = $request->get( 'parent_id', '');$taxon['parent_id'] = $parent_id;return view('admin/taxon/create', $taxon);}/*** 将新创建存储到存储器** @param Request $request* @return Response*/public function store(TaxonRequest $request){$parent = Taxon::find($request->input('parent_id'));$taxon = Taxon::create($request->fillData());$taxon->makeChildOf($parent);return redirect('admin/taxons');}


修改如果只修改数据,不修改分类结构就按照laravel修改就可以,

/*** 显示编辑表单页面** @param int $id* @return Response*/public function edit($id){$taxon = $this->dispatch(new TaxonFormFields($id));return view('admin/taxon/edit', $taxon);}/*** 在存储器中更新** @param Request $request* @param int $id* @return Response*/public function update(TaxonRequest $request, $id){$taxon = Taxon::findOrFail($id);$taxon = $taxon->fill($request->fillData());$taxon->save();return redirect('admin/taxons');}


删除

/*** 从存储器中移除** @param int $id* @return Response*/public function destroy($id){$taxon = Taxon::findOrFail($id);$taxon->delete();return response()->json(['error_num' => '0', 'link' => '/admin/taxons']);}




这篇关于laravel集成baum的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/802487

相关文章

SpringBoot集成LiteFlow实现轻量级工作流引擎的详细过程

《SpringBoot集成LiteFlow实现轻量级工作流引擎的详细过程》LiteFlow是一款专注于逻辑驱动流程编排的轻量级框架,它以组件化方式快速构建和执行业务流程,有效解耦复杂业务逻辑,下面给大... 目录一、基础概念1.1 组件(Component)1.2 规则(Rule)1.3 上下文(Conte

使用vscode搭建pywebview集成vue项目实践

《使用vscode搭建pywebview集成vue项目实践》:本文主要介绍使用vscode搭建pywebview集成vue项目实践,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地... 目录环境准备项目源码下载项目说明调试与生成可执行文件核心代码说明总结本节我们使用pythonpywebv

Maven项目中集成数据库文档生成工具的操作步骤

《Maven项目中集成数据库文档生成工具的操作步骤》在Maven项目中,可以通过集成数据库文档生成工具来自动生成数据库文档,本文为大家整理了使用screw-maven-plugin(推荐)的完... 目录1. 添加插件配置到 pom.XML2. 配置数据库信息3. 执行生成命令4. 高级配置选项5. 注意事

Java集成Onlyoffice的示例代码及场景分析

《Java集成Onlyoffice的示例代码及场景分析》:本文主要介绍Java集成Onlyoffice的示例代码及场景分析,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要... 需求场景:实现文档的在线编辑,团队协作总结:两个接口 + 前端页面 + 配置项接口1:一个接口,将o

Swagger2与Springdoc集成与使用详解

《Swagger2与Springdoc集成与使用详解》:本文主要介绍Swagger2与Springdoc集成与使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐... 目录1. 依赖配置2. 基础配置2.1 启用 Springdoc2.2 自定义 OpenAPI 信息3.

Spring Boot 集成 Solr 的详细示例

《SpringBoot集成Solr的详细示例》:本文主要介绍SpringBoot集成Solr的详细示例,本文通过实例代码给大家介绍的非常详细,感兴趣的朋友一起看看吧... 目录环境准备添加依赖配置 Solr 连接定义实体类编写 Repository 接口创建 Service 与 Controller示例运行

Spring Boot集成SLF4j从基础到高级实践(最新推荐)

《SpringBoot集成SLF4j从基础到高级实践(最新推荐)》SLF4j(SimpleLoggingFacadeforJava)是一个日志门面(Facade),不是具体的日志实现,这篇文章主要介... 目录一、日志框架概述与SLF4j简介1.1 为什么需要日志框架1.2 主流日志框架对比1.3 SLF4

Spring Boot集成Logback终极指南之从基础到高级配置实战指南

《SpringBoot集成Logback终极指南之从基础到高级配置实战指南》Logback是一个可靠、通用且快速的Java日志框架,作为Log4j的继承者,由Log4j创始人设计,:本文主要介绍... 目录一、Logback简介与Spring Boot集成基础1.1 Logback是什么?1.2 Sprin

springboot集成Lucene的详细指南

《springboot集成Lucene的详细指南》这篇文章主要为大家详细介绍了springboot集成Lucene的详细指南,文中的示例代码讲解详细,具有一定的借鉴价值,感兴趣的小伙伴可以跟随小编一起... 目录添加依赖创建配置类创建实体类创建索引服务类创建搜索服务类创建控制器类使用示例以下是 Spring

Spring Boot 集成 Quartz并使用Cron 表达式实现定时任务

《SpringBoot集成Quartz并使用Cron表达式实现定时任务》本篇文章介绍了如何在SpringBoot中集成Quartz进行定时任务调度,并通过Cron表达式控制任务... 目录前言1. 添加 Quartz 依赖2. 创建 Quartz 任务3. 配置 Quartz 任务调度4. 启动 Sprin