Laravel 表格与模型

Laravel forms with models

我的表单有此代码并且运行良好:

    <div class="form-group">

        <select class="form-control" id="exampleSelect1">
        <option>Assign</option>
        @foreach ($projects as $project)
        <option>{{ $project->name }} </option>

        @endforeach
        </select>
    </div>

但我想做的是使用联系表:

        <div class="form-group">
            {!! Form::label('name', 'Project') !!}
            {!! Form::select (@theforeach with the values :c ) !!}}
        </div>

我正在使用 App\Http\Requests\ContactFormRequest; 并且我一直在寻找这样做的方法,但是 google 上的例子很少。

试试这个

<div class="form-group">
    {!! Form::label('project', 'Project') !!}
    {!! Form::select ('project', $projects->pluck('name')) !!}} 
</div>

查看文档 https://laravel.com/docs/4.2/html#drop-down-lists

<div class="form-group">
    {!! Form::label('project', 'Project') !!}
    {!! Form::select ('project', $projects->pluck('name', 'id')) !!}}
</div>

Form is part of the Laravel Collective HTML library, you can find the documentation here, specifically you're looking for the Select 文档:

echo Form::select('size', ['L' => 'Large', 'S' => 'Small']);

您有一个 Project 模型的集合,每个模型都有一个 name 和(大概)一个 id,您需要将其转换为键 -> 值数组 select 方法,你可以使用 pluck:

{!! Form::select('project', $projects->pluck('name', 'id')) !!}

然后在您的控制器中,您将能够找到使用 find 选择的项目,例如:

Project::find($request->project);

如果您希望 select 选项起作用,您需要正确调用它,

来自控制器,

 // Example : This is for single view page

 $list_of_options = Products::pluck('name','id');
 return view('your_view_name',compact('list_of_options')); 

// Example : If you want select dropdown in all page ( within the controller views)   then,

use Illuminate\Support\Facades\View;

public function __construct(){
   View::share('list_of_options',Products::pluck('name','id'));  
}

现在 blade,

  {{ dd($list_of_options); }} // Check if the values are comming in proper formate

  {!! Form::select('name_of_the_select', $list_of_options, null ,[
         'class' => 'form-control',
         'id'    => 'name_of_the_select'
      ]);
   !!}

这是里面有 <i><span> 的按钮 :-

{{ Form::button(
    '<span class="fa fa-play fa-1x"></span>',
        [
            'class'=>'btn btn-info',
            'type'=>'button'
        ])
    }}

根据你的问题(已更新

<div class="form-group">
{!! Form::label('exampleSelect1', 'Project') !!}
{!! Form::select('projects', $projects, null ,[
             'class'       => 'form-control',
             'id'          => 'exampleSelect1',
             'placeholder' => 'Please select project'

          ]);
       !!}
    </div>

希望对您有所帮助。 :)