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

相关文章

Spring Boot集成/输出/日志级别控制/持久化开发实践

《SpringBoot集成/输出/日志级别控制/持久化开发实践》SpringBoot默认集成Logback,支持灵活日志级别配置(INFO/DEBUG等),输出包含时间戳、级别、类名等信息,并可通过... 目录一、日志概述1.1、Spring Boot日志简介1.2、日志框架与默认配置1.3、日志的核心作用

Apache Ignite 与 Spring Boot 集成详细指南

《ApacheIgnite与SpringBoot集成详细指南》ApacheIgnite官方指南详解如何通过SpringBootStarter扩展实现自动配置,支持厚/轻客户端模式,简化Ign... 目录 一、背景:为什么需要这个集成? 二、两种集成方式(对应两种客户端模型) 三、方式一:自动配置 Thick

OpenCV在Java中的完整集成指南分享

《OpenCV在Java中的完整集成指南分享》本文详解了在Java中集成OpenCV的方法,涵盖jar包导入、dll配置、JNI路径设置及跨平台兼容性处理,提供了图像处理、特征检测、实时视频分析等应用... 目录1. OpenCV简介与应用领域1.1 OpenCV的诞生与发展1.2 OpenCV的应用领域2

SpringBoot集成MyBatis实现SQL拦截器的实战指南

《SpringBoot集成MyBatis实现SQL拦截器的实战指南》这篇文章主要为大家详细介绍了SpringBoot集成MyBatis实现SQL拦截器的相关知识,文中的示例代码讲解详细,有需要的小伙伴... 目录一、为什么需要SQL拦截器?二、MyBATis拦截器基础2.1 核心接口:Interceptor

SpringBoot集成EasyPoi实现Excel模板导出成PDF文件

《SpringBoot集成EasyPoi实现Excel模板导出成PDF文件》在日常工作中,我们经常需要将数据导出成Excel表格或PDF文件,本文将介绍如何在SpringBoot项目中集成EasyPo... 目录前言摘要简介源代码解析应用场景案例优缺点分析类代码方法介绍测试用例小结前言在日常工作中,我们经

Spring Boot集成Druid实现数据源管理与监控的详细步骤

《SpringBoot集成Druid实现数据源管理与监控的详细步骤》本文介绍如何在SpringBoot项目中集成Druid数据库连接池,包括环境搭建、Maven依赖配置、SpringBoot配置文件... 目录1. 引言1.1 环境准备1.2 Druid介绍2. 配置Druid连接池3. 查看Druid监控

在Spring Boot中集成RabbitMQ的实战记录

《在SpringBoot中集成RabbitMQ的实战记录》本文介绍SpringBoot集成RabbitMQ的步骤,涵盖配置连接、消息发送与接收,并对比两种定义Exchange与队列的方式:手动声明(... 目录前言准备工作1. 安装 RabbitMQ2. 消息发送者(Producer)配置1. 创建 Spr

如何在Spring Boot项目中集成MQTT协议

《如何在SpringBoot项目中集成MQTT协议》本文介绍在SpringBoot中集成MQTT的步骤,包括安装Broker、添加EclipsePaho依赖、配置连接参数、实现消息发布订阅、测试接口... 目录1. 准备工作2. 引入依赖3. 配置MQTT连接4. 创建MQTT配置类5. 实现消息发布与订阅

SpringBoot集成LiteFlow工作流引擎的完整指南

《SpringBoot集成LiteFlow工作流引擎的完整指南》LiteFlow作为一款国产轻量级规则引擎/流程引擎,以其零学习成本、高可扩展性和极致性能成为微服务架构下的理想选择,本文将详细讲解Sp... 目录一、LiteFlow核心优势二、SpringBoot集成实战三、高级特性应用1. 异步并行执行2

SpringBoot3应用中集成和使用Spring Retry的实践记录

《SpringBoot3应用中集成和使用SpringRetry的实践记录》SpringRetry为SpringBoot3提供重试机制,支持注解和编程式两种方式,可配置重试策略与监听器,适用于临时性故... 目录1. 简介2. 环境准备3. 使用方式3.1 注解方式 基础使用自定义重试策略失败恢复机制注意事项