model
Generate Eloquent model for CatchAdmin module with full CatchModel features.
Install
mkdir -p .claude/skills/model && curl -L -o skill.zip "https://mcp.directory/api/skills/download/5552" && unzip -o skill.zip -d .claude/skills/model && rm skill.zipInstalls to .claude/skills/model
About this skill
Step 3: Generate Model
创建 Eloquent 模型,继承 Catch\Base\CatchModel。
File Location
modules/{Module}/Models/{Model}.php
Complete Template
<?php
namespace Modules\{Module}\Models;
use Catch\Base\CatchModel as Model;
use Catch\Enums\Status;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany;
class {Model} extends Model
{
protected $table = '{table}';
protected $fillable = [
'id',
// business fields
'creator_id',
'created_at',
'updated_at',
'deleted_at',
];
/**
* 搜索字段配置
* Operators: like, =, in, between
*/
public array $searchable = [
'name' => 'like',
'status' => '=',
'category_id' => '=',
];
/**
* 列表查询字段
*/
protected array $fields = [
'id', 'name', 'status', 'created_at'
];
/**
* 表单提交字段
*/
protected array $form = [
'name', 'status'
];
/**
* 表单关联同步 (多对多)
*/
protected array $formRelations = ['roles', 'tags'];
/**
* 排序字段
*/
protected string $sortField = 'sort';
/**
* 降序排序
*/
protected bool $sortDesc = true;
/**
* 树形结构 (parent_id)
*/
protected string $parentIdColumn = 'parent_id';
/**
* 是否返回树形结构
*/
protected bool $asTree = false;
// Relationships
public function category(): BelongsTo
{
return $this->belongsTo(Category::class, 'category_id');
}
}
CatchModel 核心属性
| 属性 | 类型 | 默认值 | 说明 |
|---|---|---|---|
$fillable | array | [] | 可填充字段 |
$searchable | array | [] | 搜索字段及操作符 |
$fields | array | ['*'] | 列表查询字段 |
$form | array | [] | 表单提交字段 |
$formRelations | array | [] | 表单关联同步 |
$sortField | string | '' | 排序字段 |
$sortDesc | bool | true | 是否降序 |
$parentIdColumn | string | 'parent_id' | 父级字段 |
$asTree | bool | false | 返回树形结构 |
$isPaginate | bool | true | 是否分页 |
$dataRange | bool | false | 数据权限 |
$isFillCreatorId | bool | true | 自动填充创建人 |
CatchModel CRUD 方法
列表查询
// 基础列表 (自动分页、搜索、排序)
$this->model->getList();
// 自定义查询条件
$this->model->setBeforeGetList(function ($query) {
return $query->where('type', 1);
})->getList();
// 禁用分页
$this->model->disablePaginate()->getList();
// 返回树形结构
$this->model->asTree()->getList();
创建记录
// 创建并返回 ID
$this->model->storeBy($data);
// 创建新实例
$this->model->createBy($data);
更新记录
// 更新单条
$this->model->updateBy($id, $data);
// 批量更新
$this->model->batchUpdate('id', [1, 2, 3], [
'status' => [1, 2, 1],
]);
删除记录
// 软删除
$this->model->deleteBy($id);
// 强制删除
$this->model->deleteBy($id, force: true);
// 批量删除
$this->model->deletesBy('1,2,3');
$this->model->deletesBy([1, 2, 3]);
其他操作
// 查询单条
$this->model->firstBy($id);
$this->model->firstBy($value, 'field');
// 切换状态
$this->model->toggleBy($id);
$this->model->toggleBy($id, 'is_active');
// 恢复软删除
$this->model->restoreBy($id);
$this->model->restoreBy('1,2,3');
搜索操作符
public array $searchable = [
'name' => 'like', // LIKE '%value%'
'title' => '%like', // LIKE '%value'
'code' => 'like%', // LIKE 'value%'
'status' => '=', // = value
'type' => 'in', // IN (values)
'created_at' => 'between', // BETWEEN start AND end
'price' => '>', // > value
'stock' => '>=', // >= value
];
关联关系
BelongsTo (多对一)
public function category(): BelongsTo
{
return $this->belongsTo(Category::class, 'category_id');
}
public function creator(): BelongsTo
{
return $this->belongsTo(\Modules\User\Models\User::class, 'creator_id');
}
HasMany (一对多)
public function items(): HasMany
{
return $this->hasMany(OrderItem::class, 'order_id');
}
BelongsToMany (多对多)
// 需要在 $formRelations 中声明才能自动同步
protected array $formRelations = ['roles', 'tags'];
public function roles(): BelongsToMany
{
return $this->belongsToMany(
Role::class,
'user_has_roles', // pivot table
'user_id',
'role_id'
);
}
public function tags(): BelongsToMany
{
return $this->belongsToMany(Tag::class, 'product_tags');
}
树形关系 (自关联)
protected string $parentIdColumn = 'parent_id';
public function parent(): BelongsTo
{
return $this->belongsTo(self::class, 'parent_id');
}
public function children(): HasMany
{
return $this->hasMany(self::class, 'parent_id');
}
属性访问器 & 修改器
use Illuminate\Database\Eloquent\Casts\Attribute;
// 密码加密
protected function password(): Attribute
{
return new Attribute(
set: fn ($value) => bcrypt($value),
);
}
// 状态标签
protected function statusLabel(): Attribute
{
return new Attribute(
get: fn () => $this->status == 1 ? '启用' : '禁用',
);
}
// JSON 字段
protected function options(): Attribute
{
return new Attribute(
get: fn ($value) => json_decode($value, true) ?? [],
set: fn ($value) => json_encode($value),
);
}
状态枚举
use Catch\Enums\Status;
public function isEnabled(): bool
{
return Status::Enable->assert($this->status);
}
public function isDisabled(): bool
{
return Status::Disable->assert($this->status);
}
重写方法
自定义更新逻辑
public function updateBy($id, array $data): mixed
{
// 密码为空时不更新
if (empty($data['password'])) {
unset($data['password']);
}
return parent::updateBy($id, $data);
}
自定义删除逻辑
public function deleteBy($id, bool $force = false, bool $softForce = false): ?bool
{
return $this->transaction(function () use ($id) {
// 删除关联数据
$this->items()->where('order_id', $id)->delete();
return parent::deleteBy($id);
});
}
查询作用域
use Illuminate\Database\Eloquent\Builder;
public function scopeActive(Builder $query): Builder
{
return $query->where('status', 1);
}
public function scopeOfType(Builder $query, int $type): Builder
{
return $query->where('type', $type);
}
// 使用
Model::active()->ofType(1)->get();
关联预加载
// 在模型中定义默认预加载
protected $with = ['category'];
// 在控制器中
public function show($id): mixed
{
return $this->model
->with(['category', 'tags'])
->firstBy($id);
}
外键字段自动检测规则
| 字段名 | 关联方法 | 关联模型 |
|---|---|---|
category_id | category() | Category::class |
user_id | user() | User::class |
department_id | department() | Departments::class |
parent_id | parent() | self::class |
More by JaguarJack
View all skills by JaguarJack →You might also like
flutter-development
aj-geddes
Build beautiful cross-platform mobile apps with Flutter and Dart. Covers widgets, state management with Provider/BLoC, navigation, API integration, and material design.
drawio-diagrams-enhanced
jgtolentino
Create professional draw.io (diagrams.net) diagrams in XML format (.drawio files) with integrated PMP/PMBOK methodologies, extensive visual asset libraries, and industry-standard professional templates. Use this skill when users ask to create flowcharts, swimlane diagrams, cross-functional flowcharts, org charts, network diagrams, UML diagrams, BPMN, project management diagrams (WBS, Gantt, PERT, RACI), risk matrices, stakeholder maps, or any other visual diagram in draw.io format. This skill includes access to custom shape libraries for icons, clipart, and professional symbols.
ui-ux-pro-max
nextlevelbuilder
"UI/UX design intelligence. 50 styles, 21 palettes, 50 font pairings, 20 charts, 8 stacks (React, Next.js, Vue, Svelte, SwiftUI, React Native, Flutter, Tailwind). Actions: plan, build, create, design, implement, review, fix, improve, optimize, enhance, refactor, check UI/UX code. Projects: website, landing page, dashboard, admin panel, e-commerce, SaaS, portfolio, blog, mobile app, .html, .tsx, .vue, .svelte. Elements: button, modal, navbar, sidebar, card, table, form, chart. Styles: glassmorphism, claymorphism, minimalism, brutalism, neumorphism, bento grid, dark mode, responsive, skeuomorphism, flat design. Topics: color palette, accessibility, animation, layout, typography, font pairing, spacing, hover, shadow, gradient."
godot
bfollington
This skill should be used when working on Godot Engine projects. It provides specialized knowledge of Godot's file formats (.gd, .tscn, .tres), architecture patterns (component-based, signal-driven, resource-based), common pitfalls, validation tools, code templates, and CLI workflows. The `godot` command is available for running the game, validating scripts, importing resources, and exporting builds. Use this skill for tasks involving Godot game development, debugging scene/resource files, implementing game systems, or creating new Godot components.
nano-banana-pro
garg-aayush
Generate and edit images using Google's Nano Banana Pro (Gemini 3 Pro Image) API. Use when the user asks to generate, create, edit, modify, change, alter, or update images. Also use when user references an existing image file and asks to modify it in any way (e.g., "modify this image", "change the background", "replace X with Y"). Supports both text-to-image generation and image-to-image editing with configurable resolution (1K default, 2K, or 4K for high resolution). DO NOT read the image file first - use this skill directly with the --input-image parameter.
fastapi-templates
wshobson
Create production-ready FastAPI projects with async patterns, dependency injection, and comprehensive error handling. Use when building new FastAPI applications or setting up backend API projects.
Related MCP Servers
Browse all serversConnect Blender to Claude AI for seamless 3D modeling. Use AI 3D model generator tools for faster, intuitive, interactiv
AI-driven CAD modeling with FreeCAD: control design workflows, generate logos, and edit objects using remote Python scri
Supercharge AI tools with Kagi MCP: fast google web search API, powerful ai summarizer, and seamless ai summary tool int
Replicate Flux is an OpenAPI image generator using Replicate's Flux model, enabling image creation via API and TypeScrip
Generate and edit images with Stability AI's powerful ai image generator using advanced Stable Diffusion models for stun
Gemini Image Generator uses AI to create images with prompt enhancement, character consistency, and multi-image blending
Stay ahead of the MCP ecosystem
Get weekly updates on new skills and servers.