php 以 Laravel 形式设置选定的选项

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38428324/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 01:34:42  来源:igfitidea点击:

Setting selected option in laravel form

phplaravellaravel-5.2

提问by Min Khant Lu

I need to give selected value like this html:

我需要像这样的 html 给出选定的值:

<select name="myselect" id="myselect">
 <option value="1">Item 1</option>
 <option value="2" selected='selected'>Item 2</option>

how can I achieve this, with laravel forms?

我怎样才能用 Laravel 形式实现这一点?

采纳答案by Raymond Cheng

use this package and check the docs:

使用这个包并检查文档:

https://laravelcollective.com/docs/5.2/html#drop-down-lists

https://laravelcollective.com/docs/5.2/html#drop-down-lists

form you html , you need use this mark

形成你的 html ,你需要使用这个标记

{!! Form::select('size', array('L' => 'Large', 'S' => 'Small'), 'S'); !!}

回答by lucasvscn

Everybody talking about you go using {!! Form::select() !!}but, if all you need is to use plain simple HTML.. here is another way to do it.

每个人都在谈论你去使用,{!! Form::select() !!}但是,如果你只需要使用简单的 HTML ......这是另一种方法。

<select name="myselect">
@foreach ($options as $key => $value)
    <option value="{{ $key }}"
    @if ($key == old('myselect', $model->option))
        selected="selected"
    @endif
    >{{ $value }}</option>
@endforeach
</select>

the old()function is useful when you submit the form and the validation fails. So that, old()returns the previously selected value.

old()当您提交表单并且验证失败时,该功能很有用。因此,old()返回先前选择的值。

回答by Chonchol Mahmud

You have to set the default option by passing a third argument.

您必须通过传递第三个参数来设置默认选项。

{{ Form::select('myselect', [1, 2], 2, ['id' => 'myselect']) }}

You can read the documentation here.

您可以在此处阅读文档

回答by Matiullah Karimi

You can do it like this.

你可以这样做。

<select class="form-control" name="resoureceName">

  <option>Select Item</option>

  @foreach ($items as $item)
    <option value="{{ $item->id }}" {{ ( $item->id == $existingRecordId) ? 'selected' : '' }}> {{ $item->name }} </option>
  @endforeach    </select>

回答by Alupotha

Another ordinary simple waythis is good if there are few options in select box

如果选择框中的选项很少,另一种普通的简单方法很好

<select name="job_status">
   <option {{old('job_status',$profile->job_status)=="unemployed"? 'selected':''}}  value="unemployed">Unemployed</option>
   <option {{old('job_status',$profile->job_status)=="employed"? 'selected':''}} value="employed">Employed</option>
</select>

回答by Hiren Makwana

Setting selected option is very simple in laravel form :

以 Laravel 形式设置 selected 选项非常简单:

{{ Form::select('number', [0, 1, 2], 2) }}

Output will be :

输出将是:

<select name="number">
  <option value="0">0</option>
  <option value="1">1</option>
  <option value="2" selected="selected">2</option>
</select>

回答by Mahedi Hasan Durjoy

            @foreach ($categories as $category)
             <option value="{{$category->id}}" 
               @foreach ($posts->postRelateToCategory as $Postcategory)
                 @if ($Postcategory->id == $category->id)
                 {{'selected="selected"'}}
                 @endif 
               @endforeach >
              {{ $category->category_name }} </option>               
            @endforeach    

回答by rashedcs

  <?php
      $items = DB::table('course')->get()->pluck('name','id');
      $selectID = 3;
  ?>

  <div class="form-group">
   {{ Form::label('course_title', 'Course Title') }}
   {!! Form::select('myselect', $items, $select, ['class' => 'form-control']) !!}
  </div>

This show similar types of following options :

这显示了类似类型的以下选项:

<select name="myselect" id="myselect">
 <option value="1">Computer Introduction</option>
 <option value="2">Machine Learning</option>
 <option value="3" selected='selected'>Python Programming</option>
 <option value="4">Networking Fundamentals</option>
 .
 .
 .
 .  
</select>

回答by MrMesees

To echo some other answers here, the code I just used with 5.6 is this

为了回应这里的其他一些答案,我刚刚在 5.6 中使用的代码是这样的

{{ Form::select('status', ['Draft' => 'Draft', 'Sent' => 'Sent', 'Paid' => 'Paid'], $model->status, ['id' => 'status']) }}

In order to be able to use the Form Helper from LaravelCollective I took a look at https://laravelcollective.com/docs/master/html#drop-down-lists

为了能够使用 LaravelCollective 的 Form Helper,我查看了https://laravelcollective.com/docs/master/html#drop-down-lists

I also had to composer require the dependency also so that I could use it in my projects

我还必须编写 require 依赖项,以便我可以在我的项目中使用它

composer require "laravelcollective/html":"^5"

Lastly I altered my config/app.phpand added the following in the $aliasesarray

最后我改变了我的config/app.php并在$aliases数组中添加了以下内容

    'Form' => Collective\Html\FormFacade::class,

https://laravelcollective.com/docs/master/htmlshould be consulted if any of the above ceases to work.

如果上述任何一项停止工作,应咨询https://laravelcollective.com/docs/master/html

回答by DILEEP THOMAS

Just Simply paste this code you will get the desired output that you needed.

只需简单地粘贴此代码,您将获得所需的输出。

 {{ Form::select ('myselect', ['1' => 'Item 1', '2' => 'Item 2'], 2 , ['id' =>'myselect']) }}` `