achillesp/laravel-crud-forms

为 Laravel 模型创建 CRUD 表单。

6.0 2024-03-14 14:19 UTC

This package is auto-updated.

Last update: 2024-09-15 18:36:57 UTC


README

这是一个 Laravel >=5.5 的包,旨在帮助轻松地为 eloquent 模型创建 CRUD(创建、读取、更新、删除)表单(以及索引页面)。它旨在作为不干扰应用其他部分的快速工具。

该包提供

  • 可在资源控制器中使用的特质
  • 一系列用于显示表单的视图

视图使用 bootstrap(v3)构建,但样式可以轻松覆盖。

安装

Composer

在命令行中运行

composer require achillesp/laravel-crud-forms

配置

此包使用一个配置文件,您可以通过将其发布到应用的配置目录来覆盖它。

php artisan vendor:publish --provider=Achillesp\CrudForms\CrudFormsServiceProvider --tag=config

使用方法

要使用此包,您需要在模型的控制器中使用特质 Achillesp\CrudForms\CrudForms 并定义您的路由。该特质提供所有必需的 Resource Controller 方法,以及用于软删除模型的恢复方法。

路由

例如,如果您有一个 Post 模型,您将定义以下路由

Route::resource('/posts', 'PostController');

控制器

然后在您的 PostController 中,您需要使用该特质并定义一个构造函数,在其中提供模型的必要详细信息。

use App\Post;
use Achillesp\CrudForms\CrudForms;

class PostController extends Controller
{
    use CrudForms;

    public function __construct(Post $post)
    {
        $this->model = $post;
    }
}

在控制器的构造函数中,您可以定义由控制器处理的属性。以下是可以定义的可用属性。

模型

这是模型,应通过依赖注入传递到构造函数中。

表单字段数组

这是一个包含所有表单中所需字段的数组。每个字段都声明为一个具有以下属性的数组:

  1. name: 这是模型在数据库中的属性名称。
  2. label: 这是表单中字段的标签。
  3. type: 将使用的表单输入字段的类型。接受类型有
    • 文本
    • 多行文本
    • 电子邮件
    • URL
    • 密码
    • 日期
    • 选择
    • 多选选择
    • 复选框
    • 多选复选框
    • 单选按钮
  4. 关系: 在选择、多选选择、单选按钮或多选复选框按钮的情况下需要此属性。您可以在此处指定模型中定义的关系名称。以下示例中,Post 模型有一个指向 categorybelongsTo 关系和一个指向 tagsbelongsToMany 关系。对于 belongsTo 关系,您可以使用选择或单选按钮(一组单选按钮)输入。对于 belongsToMany 关系,您可以使用多选选择或多选复选框输入。
  5. relFieldName: 可选。仅在存在关系时使用,用于设置显示的关联模型属性的名称(例如,在选择的选项中)。如果未定义,则默认使用的属性是 name
$this->formFields = [
    ['name' => 'title', 'label' => 'Title', 'type' => 'text'],
    ['name' => 'slug', 'label' => 'Slug', 'type' => 'text'],
    ['name' => 'body', 'label' => 'Enter your content here', 'type' => 'textarea'],
    ['name' => 'publish_on', 'label' => 'Publish Date', 'type' => 'date'],
    ['name' => 'published', 'label' => 'Published', 'type' => 'checkbox'],
    ['name' => 'category_id', 'label' => 'Category', 'type' => 'select', 'relationship' => 'category'],
    ['name' => 'tags', 'label' => 'Tags', 'type' => 'select_multiple', 'relationship' => 'tags'],
];

indexFields 数组

这是在索引页面中显示的模型属性。

$this->indexFields = ['title', 'category_id', 'published'];

如果没有定义,则显示 formFields 中的第一个。

formTitle(可选)

您可以可选地定义模型名称,以便在视图中显示。如果未定义,则使用模型名称。

bladeLayout(可选)

这用于定义将要被 CRUD 表单和索引页面视图扩展的刀片布局文件。

显示已删除模型的选项(withTrashed

将此设置为 true,将同时显示已删除模型并提供恢复它们的选项。

$this->withTrashed = true;

为了能够恢复模型,您需要定义一个额外的路由

Route::put('/posts/{post}/restore', ['as' => 'posts.restore', 'uses' => 'PostController@restore']);

validationRules 数组(可选)

这些是我们希望在保存模型之前验证数据时使用的规则。

$this->validationRules = [
    'title'       => 'required|max:255',
    'slug'        => 'required|max:100',
    'body'        => 'required',
    'publish_on'  => 'date',
    'published'   => 'boolean',
    'category_id' => 'required|int',
];

validationMessages 数组(可选)

使用此选项定义验证错误的自定义消息。例如

$this->validationMessages = [
    'body.required' => "You need to fill in the post content."
];

validationAttributes 数组(可选)

使用此选项更改属性名称在验证错误消息中的显示方式。

$this->validationAttributes = [
    'title' => 'Post title'
];

视图

视图是用 bootstrap v.3 构建的,同时也包含支持一些常见 JavaScript 库的 CSS 类。

  • 在选择输入中使用 select2 类
  • 在日期输入中使用 datepicker 类
  • 在索引视图表中使用 data-table 类

也可以发布视图,以便您可以按需更改它们。要发布它们,请使用以下 artisan 命令

php artisan vendor:publish --provider=Achillesp\CrudForms\CrudFormsServiceProvider --tag=views

许可证

MIT 许可证(MIT)。有关更多信息,请参阅许可证文件