【问题标题】:Add one more option in select element using Illuminate\html使用 Illuminate\html 在选择元素中添加另一个选项
【发布时间】:2026-02-16 22:05:01
【问题描述】:

我有一个这样的创建方法:

public function create()
{
    $categories = App\CategoryModel::pluck('name', 'id');
    return view('posts.create', compact('categories'));
}

我想添加一些选项来使用Illuminate\html 选择元素。

这是我的选择元素:

{!! Form::label('category', 'Category') !!}
{!! Form::select(null, $categories, null, ['class' => 'form', 'style' => 'height: 40px;', 'name' => 'category']); !!}

但我想再添加一个这样的选项元素:

<option disabled selected> -- Select a category -- </option>

我该怎么办?

【问题讨论】:

  • 默认功能无法做到这一点,但您可以使用Macro添加额外的表单助手
  • 如果我使用宏,就像编写 HTML 一样。你认为我应该写 HTML 还是使用宏?因为我看了一些关于它的文章,他们告诉Illuminate\html是用来减少代码的。
  • 我说的是Form::macro 而不是HTML::macro()Form::macro() 允许您使用自己的方法扩展 Form 外观。如果你想要这个,我会为你创建该函数并将其作为答案
  • 如果我写那个,我应该写哪个文件?我想写多次使用。
  • 您可以在app forlder下编写您的宏(即/app/lib/helper.php)。您还需要将其注册到 laravel autoloader...

标签: php laravel illuminate-container


【解决方案1】:

您可以创建自定义宏来实现这一点。通过代码检查以下步骤:

1) 在app/lib/macro.php

下创建宏
<?php
//My custom macro...
Form::macro('mySelect', function($name, $list = array(), $selected = null, $disabled = null, $options = array())
{
    $selected = $this->getValueAttribute($name, $selected);
    $disabled = $this->getValueAttribute($name, $disabled);

    $options['id'] = $this->getIdAttribute($name, $options);

    if ( ! isset($options['name'])) $options['name'] = $name;

    $html = array();

    foreach ($list as $list_el)
    {
        $selectedAttribute = $this->getSelectedValue($list_el['id'], $selected);
        $disabledAttribute = $this->getSelectedValue($list_el['id'], $disabled);
        $option_attr = array('value' => e($list_el['id']), 'selected' => $selectedAttribute, 'disabled' => $disabledAttribute);
        $html[] = '<option'.$this->html->attributes($option_attr).'>'.e($list_el['value']).'</option>';
    }

    $options = $this->html->attributes($options);

    $list = implode('', $html);

    return "<select{$options}>{$list}</select>";
});

2) 将宏注册到 app/Providers/MacroServiceProvider.php

<?php

namespace App\Providers;

use App\Services\Macros\Macros;
use Collective\Html\HtmlServiceProvider;

/**
 * Class MacroServiceProvider
 * @package App\Providers
 */
class MacroServiceProvider extends HtmlServiceProvider
{
    /**
     * Bootstrap the application services.
     *
     * @return void
     */
    public function boot()
    {
        //
        require base_path() . '/app/lib/macro.php';
    }

.
.
.
}

3) 使用我的自定义宏

{!!Form::mySelect('category',array(array('id' => '0', 'value'=>'-- Select a category --'), array('id' => '1', 'value'=>'My value1'), array('id' => '2', 'value'=>'My value2')), 0, 0) !!}

使用 Laravel 5.2 测试。

【讨论】:

  • @Yachi웃:你检查了吗?
  • 请按照说明执行步骤2) Register macro into app/Providers/MacroServiceProvider.php
  • 正如我在放置这个答案之前测试过的,它工作正常。
  • 我确实创建了这个文件。我用的是laravel 5.4,会不会有麻烦?
  • @Yachi웃:更新了第 2 步),检查并更正启动方法以及包含。
【解决方案2】:

您可以使用 array_merge 在 $categories 数组上添加一个新值:

{{
Form::select(
    null,
    array_merge(['' => ['label' => '-- Select a category --', 'disabled' => true], $categories),
    null,
    ['class' => 'form', 'style' => 'height: 40px;', 'name' => 'category']
}}

【讨论】:

  • 我认为你不能用 pluck 方法合并。它按我的预期返回该选项,但另一个选项具有对象的值,它是 {"6":"Science","5":"Science Fiction"}
【解决方案3】:

使用prepend() Laravel 助手:

{!! Form::label('category', 'Category') !!}
{!! Form::select(null, 
    ["value" => "Select a category", "id" => 0] + $categories, 
    null,
    ['class' => 'form', 'style' => 'height: 40px;', 'name' => 'category']) !!}

查看官方文档here了解更多信息。

希望对你有所帮助。

【讨论】:

  • 它不起作用,因为 pluck 方法返回一个对象。
  • @Yachi웃 你说的完全正确,pluck 返回一个集合,我已经更新了我的答案。