我有一个表单,用户应在其中为其项目选择一个“主题”。
<div class="form-group">
{!! Form::label('subject', 'Subject', ['class' => 'control-label']) !!}
{!! Form::select('subject',$subjects, null , ['class' => 'form-control']) !!}
</div>
这些主题是从名为“subjects”的数据库表返回的,该表有两列,一列代表“id”,一列代表“subject”。项目表中的 subject_id 引用主题表中的 id。用户在表单中选择主题名称,但我需要将主题 ID 保存在项目表中。我想在表单请求中修改此输入并用其 ID 替换主题。 表单请求中的函数是这样的:
public function prepInput(){
$input=$this->all();
$input['subject_id']=something;
$this->replace($input);
}
如何找到主题的 ID?我有一个主题模型,并且建立了该模型和项目模型之间的关系。
最佳答案
你可以使用 Eloquent lists
Controller
$subjects = Subject::lists('subject', 'id');
查看
{{ Form::select('subject', $subjects) }}
关于php - 拉拉维尔 5 : How to find the id of a row based on another field?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31618265/