简体   繁体   English

laravel填充选择框

[英]laravel populate select box

I have seen similar questions asked before, but was unable to find a solution to my problem. 我之前曾看到过类似的问题,但无法找到解决我问题的方法。 So I have a ClientController and within it this function 所以我有一个ClientController,其中有这个功能

public function edit(Client $client)
{
    return view('clients.edit', compact('client'));
}

So that passes the client object to my edit view. 这样会将客户端对象传递到我的编辑视图。 This view is pretty much the following 此视图大致如下

{!! Form::model($client, ['method' => 'PATCH', 'route' => ['clients.update', $client->slug]]) !!}
@include('clients/partials/_form', ['submit_text' => 'Edit Client'])
{!! Form::close() !!}

So it is using a partial for the form. 因此,它对表单使用了部分内容。 At the moment, the partial looks like so 此刻,部分看起来像这样

<div class="form-group">
    {!! Form::label('clientName', 'Client Name:') !!}
    {!! Form::text('clientName') !!}
</div>
<div class="form-group">
    {!! Form::label('clientStatus', 'Client Status:') !!}
    {!! Form::select('clientStatus') !!}
</div>

When I visit the edit page for a client, I can see the form. 当我访问客户的编辑页面时,可以看到表格。 The clientName is populated with the clientName value. 用clientName值填充clientName。 The clientStatus is populated if I put it as a text input, but I cant get it to populate within a select as shown above. 如果我将它作为文本输入来填充,则clientStatus将被填充,但是如上所述,我无法在选择中填充它。 Furthermore, clientStatus can either be New or Existing. 此外,clientStatus可以是New或Existing。 I need the select box to be pre-populated with the status of the client that is being edited, but I need the other option available within the select as well. 我需要选择框预先填充正在编辑的客户端的状态,但是我还需要选择中可用的其他选项。 So if the clientStatus is New, New should be pre-selected within the select box and if I open the select, Existing should be the other option. 因此,如果clientStatus为New,则应该在选择框中预先选择New,如果我打开选择,则Existing应该是另一个选项。

What would be the best way to achieve this? 实现这一目标的最佳方法是什么?

Thanks 谢谢

Modify your select to include an array of the possible values. 修改您的选择以包括可能值的数组。

Basic Select - Label is value 基本选择-标签就是价值

{!! Form::select('clientStatus',['New','Existing']) !!}

Key Value Select - Key is value 键值选择-键就是值

{!! Form::select('clientStatus',[ 1 => 'New', 2 => 'Existing']) !!}

Form model will then set the value in the select to the one in the model. 然后,表单模型将选择中的值设置为模型中的值。

More information see the docs . 有关更多信息,请参阅文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM