craigzearfoss / bullets
为Laravel 5.1 Eloquent模型添加项目符号。
1.0.0
2016-04-27 20:47 UTC
Requires
- php: >=5.5.9
- illuminate/database: 5.1.*|5.2.*
This package is not auto-updated.
Last update: 2024-09-14 17:59:02 UTC
README
此包允许您在Laravel 5中为Eloquent模型附加项目符号。
Composer安装
您可以在Packagist上找到它。推荐的方式是通过composer。
编辑composer.json
并添加
{ "require": { "craigzearfoss/bullets": "dev-master" } }
然后安装依赖
$ composer update
如果您还没有安装Composer,请运行以下两个命令
$ curl -sS https://getcomposer.org/installer | php
$ php composer.phar install
安装并运行迁移
在config/app.php
中找到providers
数组键并注册Bullets Service Provider
。
'providers' => array( // ... Craigzearfoss\Bullets\BulletsServiceProvider::class, )
运行迁移以创建bullets
表。
php artisan vendor:publish --provider="Craigzearfoss\Bullets\Providers\BulletsServiceProvider"
php artisan migrate
配置
在您的模型中添加BulletableTrait。
<?php // ... use Craigzearfoss\Bullets\BulletableTrait; class MyModel extends Model { use BulletableTrait;
用法
要获取模型的项目符号
$bullets = $myModel->bullets()->get();
在存储或更新模型时同步模型的项目符号
$myModel->syncBullets(isset($data['bullet_list']) ? $data['bullet_list'] : []);
要将模型的项目符号添加到您的表单blade模板中
- 添加表单选择元素。
<div class="form-group"> {!! Form::label('bullet_list', 'Bullet Points:') !!} {!! Form::select('bullet_list[]', $myModel->bullets()->lists('comment', 'comment')->toArray(), array_keys($myModel->bullets()->lists('comment', 'comment')->toArray()), ['id' => 'bullet_list', 'class' => 'form-control select2-bullet-list', 'multiple']) !!} </div>
- 将select.js添加到您的布局中。
<script type="text/javascript" src="js/select2.min.js"></script>
- 将以下内容添加到您的CSS中,为表单元素。这使项目符号选择列表项的宽度为100%。
.select2-bullet-list + .select2-container--default .select2-selection--multiple .select2-selection__choice { width: 100%; } ``` 4. Add the following JavaScript to the form page. ```javascript <script type="text/javascript"> $("#bullet_list").select2({ placeholder: "Add Bullet Points", tags: true, tokenSeparators: ["\n"], maximumSelectionLength: 255 }); </script>
- 在blade模板中显示模型的项目符号
@if (!empty($bullets)) <ul> @foreach($bullets as $bullet) <li>{{ $bullet->comment }}</li> @endforeach </ul> @endif
变更日志
支持
贡献者行为准则
请注意,本项目按照贡献者行为准则发布。参与本项目即表示您同意遵守其条款。
许可证
Bullets采用MIT许可证发布。有关详细信息,请参阅捆绑的LICENSE文件。