erdemozveren/laravelmacros

Laravel项目助手宏

v0.3.2 2020-07-12 00:07 UTC

This package is auto-updated.

Last update: 2024-09-13 04:09:05 UTC


README

Latest Version on Packagist Total Downloads Build Status StyleCI

因为我比较懒,所以制作了这个Laravel助手宏的包,其中包含了一些实用的快捷方式。

兼容性:Laravel 5.8+ ,不适用于生产环境

使用方法

安装

通过Composer安装

$ composer require erdemozveren/laravelmacros

为了定制,首先发布配置文件

$ php artisan vendor:publish --provider="erdemozveren\laravelmacros\LaravelMacrosServiceProvider" --tag=config

现在你可以编辑默认的输入、设置等配置了。

入门指南

laravelmacros旨在构建表单和表单处理的简单骨架,为了开始使用,在你的模型中加入 use erdemozveren\laravelmacros\Traits\FormBuilderTrait;,然后在类中使用该特质 use FormBuilderTrait;,现在你可以继续了!

此包添加了Laravel集体宏以简化操作,你可以通过简单地将 "c" 添加到开头并使下一个字母大写来使用它,例如:

Form::text('email', 'example@gmail.com') --> Form::cText('email', 'example@gmail.com')

使用方法

表单构建器

在模型中添加formFields函数

public function formFields() {
    return [
        "*"=>[ // wildcard will be applied to all elements 
            "options"=>
            ["style"=>"color:red!important"]
        ],
        "parent_id"=>[ // "parent_id" is the name attribute
            "type"=>"select", // input type (e.g. "select" will look for "cSelect")
            "label"=>"Parent", // label text
            "data"=>User::pluck('full_name','id'), // [ONLY FOR select] you can write any data source that laravel collective accepts
            "options"=>[ // optional
                "required"=>false // optional make input optional
                // ... and other "laravel collective" options and dom parameters can be used in here
            ]
        ],
        "full_name"=>[
            "type"=>"text",
            "label"=>"Full Name",
        ],
        "email"=>[
            "type"=>"text",
            "label"=>"E-mail",
        ],
        "password"=>[
            "type"=>"password",
            "label"=>"Password",
        ],
    ];
}

生成表单

在你的blade文件中可以这样使用

{!!Form::model($model,['url'=>"/post"])!!}
{!!$model->generateForm()!!}
{!!Form::cSubmit()!!}
{!!Form::close()!!}

或者

{!!Form::open(['url'=>"/post"])!!}
{!!$model->generateForm()!!}
{!!Form::cSubmit()!!}
{!!Form::close()!!}

如果你想在一个表单中排除一个或多个元素,只需传递一个包含字段名的数组选项 _exclude

{!!$model->generateForm(["_exclude"=>["full_name","another_filed_name"]])!!}

自动生成表单字段

$ php artisan laravelmacros:formfields {tablename}

它会问你一些问题,然后给你一个 formFields 函数代码,你可以将其复制到模型文件中

表单宏

这些宏扩展自 laravel collective,也可以用于模型绑定。

输入的值将按顺序是:

  1. 会话闪存数据(旧输入
  2. 当前请求的数据(通过 Request::input 方法)
  3. 显式传递的值
  4. 模型属性数据
// variable names stand for paramaters with default values

Form::cText($name,$label,$placeholder=null,$options=[]);

Form::cColor($name,$label,$options=[]);

Form::cTextarea($name,$label,$placeholder=null,$options=[]);

Form::cNumber($name,$label,$options=[]);

Form::cEmail($name,$label,$placeholder=null,$options=[]);

Form::cPassword($name,$label,$placeholder="*******",$options=[]);

Form::cFile($name,$label,$options=[]);

Form::cCheckbox($name,$label,$value=1,$checked=null);

Form::cRadio($name,$label,$value,$checked=null);

Form::cSubmit($label="Submit",$class="");

Form::cSelect($name,$label,$data,$options=[]);

变更日志

请参阅changelog了解最近的变化。

贡献

请参阅contributing.md了解详细信息和一个待办事项列表。

安全

如果你发现任何与安全相关的问题,请通过作者邮箱而不是问题跟踪器来联系。

致谢

许可证

MIT