dlogon / quick-crud-for-laravel
分步骤创建模型CRUD
Requires
- php: ^8.0
- dlogon/tailwind-alerts: ^0.2
- spatie/laravel-package-tools: ^1.14.0
Requires (Dev)
- larastan/larastan: ^2.0.1
- laravel/pint: ^1.0
- nunomaduro/collision: ^7.8
- orchestra/testbench: ^8.8
- pestphp/pest: ^2.20
- pestphp/pest-plugin-arch: ^2.0
- pestphp/pest-plugin-laravel: ^2.0
- phpstan/extension-installer: ^1.1
- phpstan/phpstan-deprecation-rules: ^1.0
- phpstan/phpstan-phpunit: ^1.0
- phpunit/phpunit: ^10.5
- spatie/laravel-ray: ^1.26
This package is auto-updated.
Last update: 2024-09-08 15:54:14 UTC
README
分步骤创建模型CRUD
需求
此包使用TailwindCSS(https://tailwind.org.cn/)进行样式设计。它使用了一些Laravel breeze/jetstream组件和样式。
安装
您可以通过composer安装此包
composer require dlogon/quick-crud-for-laravel
您可以使用以下命令发布配置文件
php artisan vendor:publish --tag="quick-crud-for-laravel-config"
这是发布配置文件的内容
<?php return [ /* |-------------------------------------------------------------------------- | models folder |-------------------------------------------------------------------------- | | | */ 'models_folder' => [ 'App\\Models\\' => app_path('Models'), ], 'route_file_name' => 'quickcrud.php', ];
可选地,您可以使用以下命令发布视图
php artisan vendor:publish --tag="quick-crud-for-laravel-views"
使用方法
一旦您创建了模型并运行了迁移,您就可以运行以下命令
php artisan quickcrud:all <your-model-name>
这将创建
-一个名为 <your model name>Controller 的控制器
-一个位于 views/crudable/<your model in lower case>/index.blade.php 的 index 视图
-一个位于 views/crudable/<your model in lower case>/show.blade.php 的 show 视图
-一个名为 quickcrud.php 的路由文件
然后您应该将 traits Dlogon\QuickCrudForLaravel\Traits\NavigationUtils;
添加到您的模型中,例如
namespace App\Models; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; use Dlogon\QuickCrudForLaravel\Traits\NavigationUtils; class Blog extends Model { use HasFactory; use NavigationUtils; }
然后您应该在 web.php 路由文件中包含 quickcrud.php 文件或复制生成的路由到 web.php
<?php use Illuminate\Support\Facades\Route; /* |-------------------------------------------------------------------------- | Web Routes |-------------------------------------------------------------------------- | | Here is where you can register web routes for your application. These | routes are loaded by the RouteServiceProvider and all of them will | be assigned to the "web" middleware group. Make something great! | */ Route::get('/', function () { return view('welcome'); }); include __DIR__.'/quickcrud.php';
按照这些步骤,您现在可以通过您的host/<your model name in lower and plural> 访问并看到带有搜索和操作按钮的表格索引视图
如果您的模型不在默认的 App/Models/
命名空间中,您可以通过传递第二个参数传递命名空间
php artisan quickcrud:all artisan quickcrud:all Test1 App\\Models\\deep\\
示例
我们使用此迁移生成 Blog 模型
public function up(): void { Schema::create('blogs', function (Blueprint $table) { $table->id(); $table->string("name"); $table->string("content"); $table->timestamps(); }); }
然后我们运行迁移,并运行
php artisan quickcrud:all Blog
这将创建控制器文件夹中的下一个控制器
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use Dlogon\TailwindAlerts\Facades\TailwindAlerts; use Dlogon\QuickCrudForLaravel\Helpers\Search; use App\Models\Blog; class BlogController extends Controller { public $tableFields = array ( 'id' => 'id', 'name' => 'name', 'content' => 'content', 'created_at' => 'created_at', 'updated_at' => 'updated_at', ); /* [ "label" => "tablefieldName", 'label' => ["type" => "related", "field" =>"relationName.fieldNameOfRelatedModel"], 'label' => ["type"=>"money", "field"=>'moneyOrDecimalField'] ] */ public $searchFields = [ "created_at" => [ "type" => "singleDate", // search by date "label" => "Creation date", ], /* "fieldName" =>[ "type" => "text" // search by text "placeholder" => "my search" ] "customer_id" => [ "type" => "related", // search by related model "elements" => $allCustomers, //<- this will populate a select dropdown "modelDisplay" => "name", "label" => "Cliente", <--what show to "value" => "id" <-- what find to ] */ ]; /** * Display a listing of the resource. * * @return \Illuminate\Http\Response */ public function index($models = null) { $models = $models ?? Blog::orderBy("id")->paginate(10); $fields = $this->tableFields; $searchFields = $this->searchFields; return view( "crudable.blog.index", [ "models" => $models, "fields" => $fields, "searchFields" => $searchFields ] ); } public function search(Request $request) { $models = Blog::query(); if ($request->has('q')) $models = Search::searchByQueryParams(new Blog, $request)->get(); return $this->index($models); } /** * Display the specified resource. * * @param \App\Models\Blog $model * @return \Illuminate\Http\Response */ public function show(Blog $blog) { $fields = $this->tableFields; return view( "crudable.blog.show", [ "model" => $blog, "fields" => $fields ] ); } /** * Remove the specified resource from storage. * * @param \App\Models\Customer $customer * @return \Illuminate\Http\Response */ public function destroy(Blog $blog) { $blog->delete(); TailwindAlerts::addBottomToastMessage("Deleted", TailwindAlerts::ERROR); return redirect()->route("blogs.index"); } }
以及索引和显示视图。
表格字段
我们在最近创建的索引视图中有一个 table
组件,连接到控制器变量 $tablefields
。
我们应该传递一个键作为表格组件中显示的标签列的键,值作为数据库模型中的列名。
示例
["Creation date" => "created_at"]
您可以选择传递一个数组代替数据库模型列,如果这样做,您应该传递类型和字段结构。
['label' => ["type" => "related|money", "field" =>"relationName.fieldNameOfRelatedModel|numericMoneyField"]],
如果将类型设置为相关,您应该在字段 relationName.fieldName
中放置 fieldName,其中 fieldName 是相关模型中的列名
示例
['Post title' => ["type" => "related", "field" =>"post.title"]],
如果将类型设置为货币,您应该将数字字段放在数组的字段键中
示例
['Price' => ["type" => "money", "field" =>"price"]],
搜索字段
默认情况下,控制器变量 $searchfields
有一个 created_at 字段用于搜索,您可以定义以下 3 种搜索字段结构
文本
"fieldName" =>[ "type" => "text" "placeholder" => "my search" ]
where
- fieldName: 是模型中的列名
- type: 搜索定义的类型为文本
- placeholder: 在文本输入中显示的内容
日期
"fieldName" =>[ "type" => "singleDate" "label" => "my date" ]
where
- fieldName: 是模型中的 datetime 列名
- type: 日期类型
- label: 输入日期旁边的标签
相关
这将显示一个下拉菜单,用于搜索相关模型
"fieldName" => [ "type" => "related", "elements" => $models, "modelDisplay" => "name", "label" => "Comments", "value" => "id" ]
where
- fieldName: 您当前模型中包含的外键
- type: 相关类型
- elements: 搜索的相关模型,例如如果您生成一个 Invoice quickcrud,并希望搜索与客户相关的发票,您应该在这里放置 Customer::all()
- modelDisplay: 您想要显示的相关模型的字段
- label: 输入下拉旁边的标签
- value: 在相关模型搜索中搜索的相关列
变更日志
请参阅变更日志获取更多关于最近更改的信息。
贡献
请参阅贡献指南以获取详细信息。
安全漏洞
请查看我们的安全策略了解如何报告安全漏洞。
致谢
许可证
MIT许可证(MIT)。请参阅许可证文件获取更多信息。