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集成图片验证码框架easy-captcha的详细过程

《SpringBoot集成图片验证码框架easy-captcha的详细过程》本文介绍了如何将Easy-Captcha框架集成到SpringBoot项目中,实现图片验证码功能,Easy-Captcha是... 目录SpringBoot集成图片验证码框架easy-captcha一、引言二、依赖三、代码1. Ea

C#集成DeepSeek模型实现AI私有化的流程步骤(本地部署与API调用教程)

《C#集成DeepSeek模型实现AI私有化的流程步骤(本地部署与API调用教程)》本文主要介绍了C#集成DeepSeek模型实现AI私有化的方法,包括搭建基础环境,如安装Ollama和下载DeepS... 目录前言搭建基础环境1、安装 Ollama2、下载 DeepSeek R1 模型客户端 ChatBo

JAVA集成本地部署的DeepSeek的图文教程

《JAVA集成本地部署的DeepSeek的图文教程》本文主要介绍了JAVA集成本地部署的DeepSeek的图文教程,包含配置环境变量及下载DeepSeek-R1模型并启动,具有一定的参考价值,感兴趣的... 目录一、下载部署DeepSeek1.下载ollama2.下载DeepSeek-R1模型并启动 二、J

Docker部署Jenkins持续集成(CI)工具的实现

《Docker部署Jenkins持续集成(CI)工具的实现》Jenkins是一个流行的开源自动化工具,广泛应用于持续集成(CI)和持续交付(CD)的环境中,本文介绍了使用Docker部署Jenkins... 目录前言一、准备工作二、设置变量和目录结构三、配置 docker 权限和网络四、启动 Jenkins

Qt 中集成mqtt协议的使用方法

《Qt中集成mqtt协议的使用方法》文章介绍了如何在工程中引入qmqtt库,并通过声明一个单例类来暴露订阅到的主题数据,本文通过实例代码给大家介绍的非常详细,感兴趣的朋友一起看看吧... 目录一,引入qmqtt 库二,使用一,引入qmqtt 库我是将整个头文件/源文件都添加到了工程中进行编译,这样 跨平台

Debezium 与 Apache Kafka 的集成方式步骤详解

《Debezium与ApacheKafka的集成方式步骤详解》本文详细介绍了如何将Debezium与ApacheKafka集成,包括集成概述、步骤、注意事项等,通过KafkaConnect,D... 目录一、集成概述二、集成步骤1. 准备 Kafka 环境2. 配置 Kafka Connect3. 安装 D

Spring AI集成DeepSeek的详细步骤

《SpringAI集成DeepSeek的详细步骤》DeepSeek作为一款卓越的国产AI模型,越来越多的公司考虑在自己的应用中集成,对于Java应用来说,我们可以借助SpringAI集成DeepSe... 目录DeepSeek 介绍Spring AI 是什么?1、环境准备2、构建项目2.1、pom依赖2.2

Java中Springboot集成Kafka实现消息发送和接收功能

《Java中Springboot集成Kafka实现消息发送和接收功能》Kafka是一个高吞吐量的分布式发布-订阅消息系统,主要用于处理大规模数据流,它由生产者、消费者、主题、分区和代理等组件构成,Ka... 目录一、Kafka 简介二、Kafka 功能三、POM依赖四、配置文件五、生产者六、消费者一、Kaf

SpringCloud集成AlloyDB的示例代码

《SpringCloud集成AlloyDB的示例代码》AlloyDB是GoogleCloud提供的一种高度可扩展、强性能的关系型数据库服务,它兼容PostgreSQL,并提供了更快的查询性能... 目录1.AlloyDBjavascript是什么?AlloyDB 的工作原理2.搭建测试环境3.代码工程1.

SpringBoot使用注解集成Redis缓存的示例代码

《SpringBoot使用注解集成Redis缓存的示例代码》:本文主要介绍在SpringBoot中使用注解集成Redis缓存的步骤,包括添加依赖、创建相关配置类、需要缓存数据的类(Tes... 目录一、创建 Caching 配置类二、创建需要缓存数据的类三、测试方法Spring Boot 熟悉后,集成一个外