genealabs/laravel-casts
最新稳定版本:0.13.3
Composer 安装命令:
composer require genealabs/laravel-casts
包简介
Useful form macros for Laravel's Blade template engine.
README 文档
README
Casts for Laravel
Requirements
- PHP >= 7.1.3
- Laravel >= 5.5
jQueryneeds to be already loaded in your projectBootstrap JS(3 or 4) needs to be already loaded, if being used
Rationale
This package is an effort to streamline and make form development more efficient in Laravel Blade views.
Features
- Support for common CSS frameworks. (At this time only Bootstrap 3 and Bootstrap 4.beta, more coming soon.)
- Blade directives for elegant Blade view integration, providing concise syntax, reducing form boilerplate 10-fold.
- DateTime Picker for Bootstrap 3.
Installation
- Install the package:
composer require genealabs/laravel-casts
- Publish the required assets:
php artisan casts:publish --assets
Usage
The following shows all elements using Bootstrap 3:
Blade Directives
The following Blade directives developing forms a snap:
@form(['url' => '']) @errors("intro text", ["html" => "attributes"]) @text('text', '', ['placeholder' => 'Placeholder Text', 'label' => 'Text Input']) @number('number', '', ['placeholder' => 'Placeholder Text', 'label' => 'Number Input']) @password('password', ['placeholder' => 'Placeholder Text', 'label' => 'Password Input']) @date('date', '', ['placeholder' => 'Placeholder Text', 'label' => 'Date']) @datetime('datetime', '', ['placeholder' => 'Placeholder Text', 'label' => 'DateTime']) @email('email', '', ['placeholder' => 'Placeholder Text', 'label' => 'Email Input']) @url('url', '', ['placeholder' => 'Placeholder Text', 'label' => 'Url Input']) @search ('search', 'search term', ['placeholder' => 'Placeholder Text', 'label' => 'Search Input']) @color ('color', '#ff0000', ['placeholder' => 'Placeholder Text', 'label' => 'Color Input']) @range ('range', 5, ['placeholder' => 'Placeholder Text', 'label' => 'Range Input']) @tel ('tel', '1234567890', ['placeholder' => 'Placeholder Text', 'label' => 'Tel Input']) @week ('week', 3, ['placeholder' => 'Placeholder Text', 'label' => 'Week Input']) @month ('month', 'January', ['placeholder' => 'Placeholder Text', 'label' => 'Month Input']) @file('file', ['placeholder' => 'Placeholder Text', 'label' => 'File Input']) @textarea('textarea', '', ['placeholder' => 'Placeholder Text', 'label' => 'Textarea', 'rows' => 7]) @checkbox('checkbox', 'test', true, ['placeholder' => 'Placeholder Text', 'label' => 'Checkbox']) @radio('radio', 'test', true, ['placeholder' => 'Placeholder Text', 'label' => 'Radio']) @switch('fieldname', 'true value', $isChecked, ['label' => 'Switch', 'label' => 'Switch', 'onText' => 'Available', 'onColor' => 'success', 'offText' => 'Unavailable', 'offColor' => 'danger']) @select('select', [1, 2, 3, 4], null, ['placeholder' => 'Placeholder Text', 'label' => 'Select']) @selectMonths('select_months', null, ['placeholder' => 'Placeholder Text', 'label' => 'Select']) @selectWeekdays('select_weekdays', null, ['placeholder' => 'Placeholder Text', 'label' => 'Select Weekdays']) @selectRange('selectRange', 1, 21, null, ['class' => 'form-control', 'placeholder' => 'Placeholder Text', 'label' => 'Select Range']) @selectRangeWithInterval('selectRangeWithInterval', 1, 21, 3, null, ['class' => 'form-control', 'placeholder' => 'Placeholder Text', 'label' => 'Select Range With Interval']) @combobox('combobox', [1, 2, 3, 4], null, ['class' => 'form-control', 'placeholder' => 'Placeholder Text', 'label' => 'Combobox']) @submit('submit', ['class' => 'btn btn-success', 'label' => 'Submit Button']) @endform
@form / {!! Form::open() !!}
@form ($model, ['class' => '', 'labelWidth' => '', 'fieldWidth' => '', 'framework' => ''])
$model: (optional) used to pre-populate a model-based form.class: (optional)offset: (optional, default: 3)labelWidth: (optional, default: 3)fieldWidth: (optional, default: 9)framework: (optional)
@errors / {!! Form::errors() !!}
@errors (string $intro = "", array $options = [])
$intro: an optional brief pre-text to the errors list.$options: (default: []) will be rendered as html tag attributes.
@label / {!! Form::label() !!}
@label ($name, $label = null, array $options = [], $escapeHtml = true)
$name: used for thefor=attribute on the label.$label: (default: null) the label text.$options: (default: []) array of options, includingclass.$escapeHtml: (default: true) set this to false if your label should render HTML.
Note: labels are rendered automatically when using frameworks.
Input controls
The following controls use the same syntax:
@text/{!! Form::text() !!}@date/{!! Form::date() !!}@datetime/{!! Form::datetime() !!}@email/{!! Form::email() !!}@url/{!! Form::url() !!}@textarea/{!! Form::textarea() !!}
@text ($name, $value = null, $options = [])
$name: name of the control, used to submit form values.$value: (default: null) value of the control, if you need to prepopulate it. It will automatically populate old form values if validation fails.$options: (default: []) array of options, includingclass.
@switch / {!! Form::switch() !!}
@switch ('fieldname', 'active value', $isActivated, [label' => 'Switch', 'onText' => 'Available', 'offText' => 'Unavailable', 'onColor' => 'success', 'offColor' => 'danger'])
@select / {!! Form::select() !!}
@select ($name, $list = [], $selected = null, $options = [])
$name: name of the control, used to submit form values.$list: (default: []) array of key-value pairs used to create the select list options.$selected: (default: null) the value of the selected options.$options: (default: []) array of options, includingclass.
@selectMonths / {!! Form::selectMonths() !!}
@selectMonths ($name, $selected = null, $options = [])
$name: name of the control, used to submit form values.$selected: (default: null) the value of the selected options.$options: (default: []) array of options, includingclass.
@selectWeekdays / {!! Form::selectWeekdays() !!}
@selectWeekdays ($name, $selected = null, $options = [])
$name: name of the control, used to submit form values.$selected: (default: null) the value of the selected options.$options: (default: []) array of options, includingclass.
@selectRangeWithInterval / {!! Form::selectRangeWithInterval() !!}
@selectRangeWithInterval(string $name, int $start, int $end, int $interval, int $value = null, array $options = [])
$name: name of the control, used to submit form values.$start: starting value of the list.$end: ending value of the list.$interval: interval value.$value: selected value.$options: (default: []) array of options, includingclass.
CSS Framework Support
Taking advantage of framework support will handle all error validation presentation, as well as generation of labels and all the necessary form control boilerplate.
Additional framework support is planned, possibly including:
- Foundation
- Skeleton
- Semantic UI
If there's a framework you would like included, feel free to reach out by opening an issue with the pertinent details.
Bootstrap 3
To have a form render for Bootstrap 3, specify the framework and form
orientation in the @form directive:
@form(['url' => '', 'class' => 'form-horizontal', 'framework' => 'bootstrap3'])
Additionally, to enable the date/time picker, add the following to your JavaScript file for the page rendering the date fields:
if (window.$) { $(function () { if ( $.isFunction($.fn.datetimepicker) ) { $('input[type=date]').datetimepicker({ format: 'LL' }); $('input[type=datetime]').datetimepicker({ format: 'LLL', sideBySide: true }); } }); }
You can include the library using Gulp and NPM (preferred), or use a CDN.
Gulp / NPM
npm install save-dev bootstrap-datetimepicker-npm- Then require the library in your
/resources/assets/js/app.jsfile (after jQuery and Bootstrap have been required):
require('bootstrap-datetimepicker-npm');
- Don't forget to include the CSS as well in
/resources/assets/sass/app.sass:
// DateTimePicker
@import "./node_modules/bootstrap-datetimepicker-npm/src/sass/bootstrap-datetimepicker-build";
- Finally, edit your
/gulpfile.jsas needed, and rungulp.
CDNs
Add the following references to the page(s) or your main layout file. You will need to have jQuery and Bootstrap libraries included as well.
<html> <head> <!-- ... --> <link rel="stylesheet" href="https://cdn.jsdelivr.net/bootstrap.datetimepicker/4.17.42/css/bootstrap-datetimepicker.min.css"> <!-- ... --> </head> <body> <!-- ... --> <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.15.0/moment.min.js"></script> <script src="https://cdn.jsdelivr.net/bootstrap.datetimepicker/4.17.42/js/bootstrap-datetimepicker.min.js"></script> </body> </html>
Bootstrap 4 (alpha 3)
Nearly the same as above:
@form(['url' => '', 'class' => 'form-horizontal', 'framework' => 'bootstrap4'])
Testing
- Add the following entry to your
phpunit.xmlconfig file:
<directory suffix="Test.php">./vendor/genealabs/laravel-casts/tests</directory>
- Run `vendor/bin/phpunit` from the base directory of your project.
genealabs/laravel-casts 适用场景与选型建议
genealabs/laravel-casts 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 232.07k 次下载、GitHub Stars 达 95, 最近一次更新时间为 2026 年 01 月 04 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 genealabs/laravel-casts 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 genealabs/laravel-casts 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 232.07k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 95
- 点击次数: 26
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-01-04
