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集成easypoi导出word换行处理过程

《springboot集成easypoi导出word换行处理过程》SpringBoot集成Easypoi导出Word时,换行符n失效显示为空格,解决方法包括生成段落或替换模板中n为回车,同时需确... 目录项目场景问题描述解决方案第一种:生成段落的方式第二种:替换模板的情况,换行符替换成回车总结项目场景s

SpringBoot集成redisson实现延时队列教程

《SpringBoot集成redisson实现延时队列教程》文章介绍了使用Redisson实现延迟队列的完整步骤,包括依赖导入、Redis配置、工具类封装、业务枚举定义、执行器实现、Bean创建、消费... 目录1、先给项目导入Redisson依赖2、配置redis3、创建 RedissonConfig 配

SpringBoot集成XXL-JOB实现任务管理全流程

《SpringBoot集成XXL-JOB实现任务管理全流程》XXL-JOB是一款轻量级分布式任务调度平台,功能丰富、界面简洁、易于扩展,本文介绍如何通过SpringBoot项目,使用RestTempl... 目录一、前言二、项目结构简述三、Maven 依赖四、Controller 代码详解五、Service

springboot2.1.3 hystrix集成及hystrix-dashboard监控详解

《springboot2.1.3hystrix集成及hystrix-dashboard监控详解》Hystrix是Netflix开源的微服务容错工具,通过线程池隔离和熔断机制防止服务崩溃,支持降级、监... 目录Hystrix是Netflix开源技术www.chinasem.cn栈中的又一员猛将Hystrix熔

MyBatis-Plus 与 Spring Boot 集成原理实战示例

《MyBatis-Plus与SpringBoot集成原理实战示例》MyBatis-Plus通过自动配置与核心组件集成SpringBoot实现零配置,提供分页、逻辑删除等插件化功能,增强MyBa... 目录 一、MyBATis-Plus 简介 二、集成方式(Spring Boot)1. 引入依赖 三、核心机制

SpringBoot集成P6Spy的实现示例

《SpringBoot集成P6Spy的实现示例》本文主要介绍了SpringBoot集成P6Spy的实现示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面... 目录本节目标P6Spy简介抛出问题集成P6Spy1. SpringBoot三板斧之加入依赖2. 修改

springboot项目中集成shiro+jwt完整实例代码

《springboot项目中集成shiro+jwt完整实例代码》本文详细介绍如何在项目中集成Shiro和JWT,实现用户登录校验、token携带及接口权限管理,涉及自定义Realm、ModularRe... 目录简介目的需要的jar集成过程1.配置shiro2.创建自定义Realm2.1 LoginReal

SpringBoot集成Shiro+JWT(Hutool)完整代码示例

《SpringBoot集成Shiro+JWT(Hutool)完整代码示例》ApacheShiro是一个强大且易用的Java安全框架,提供了认证、授权、加密和会话管理功能,在现代应用开发中,Shiro因... 目录一、背景介绍1.1 为什么使用Shiro?1.2 为什么需要双Token?二、技术栈组成三、环境

Java 与 LibreOffice 集成开发指南(环境搭建及代码示例)

《Java与LibreOffice集成开发指南(环境搭建及代码示例)》本文介绍Java与LibreOffice的集成方法,涵盖环境配置、API调用、文档转换、UNO桥接及REST接口等技术,提供... 目录1. 引言2. 环境搭建2.1 安装 LibreOffice2.2 配置 Java 开发环境2.3 配

SpringBoot集成EasyExcel实现百万级别的数据导入导出实践指南

《SpringBoot集成EasyExcel实现百万级别的数据导入导出实践指南》本文将基于开源项目springboot-easyexcel-batch进行解析与扩展,手把手教大家如何在SpringBo... 目录项目结构概览核心依赖百万级导出实战场景核心代码效果百万级导入实战场景监听器和Service(核心