php - 拉拉维尔 5 : How to find the id of a row based on another field?

标签 php mysql laravel-5

我有一个表单,用户应在其中为其项目选择一个“主题”。

<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/

相关文章:

php - 如何获取下拉列表<option>的value属性并在php代码中使用它

php - 从 dirname( __FILE__) 获取 2 个级别

mysql - 按月分组并显示剩余总天数

python - 在 Python 中使用 mysql.connector 处理格式参数失败

mysql - 获取数据多维数组 Codeigniter Mysql

php - 如何通过 .htaccess 设置 PHP 包含路径?

javascript - Ajax 调用(之前/成功)在 php 文件内不起作用

laravel - 在 Laravel 5 中跨子域保持 session

php - 搜索存储在 MySQL DB 中的 JSON

php - 我正在使用 Laravel 5 的命令总线,我不清楚如何实现验证器类