【问题标题】:Laravel 5: How to find the id of a row based on another field?Laravel 5:如何根据另一个字段查找行的 id?
【发布时间】:2015-07-25 04:08:47
【问题描述】:

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

<div class="form-group">
  {!! Form::label('subject', 'Subject', ['class' => 'control-label']) !!}
  {!! Form::select('subject',$subjects, null , ['class' => 'form-control']) !!}
</div>

这些主题是从名为“主题”的数据库表中返回的,该表包含两列,一列用于“id”,另一列用于“主题”。项目表中的 subject_id 引用了主题表中的 id。用户在表单中选择了一个主题名称,但我需要将主题 ID 保存在项目表中。我想在表单请求中修改此输入并用其 ID 替换主题。 表单请求中的函数是这样的:

public function prepInput(){
        $input=$this->all();
        $input['subject_id']=something;
        $this->replace($input);
    }

如何找到主题的 ID?我有一个主题模型,这个模型和项目模型之间的关系已经建立。

【问题讨论】:

    标签: php mysql laravel-5


    【解决方案1】:

    你可以用雄辩的lists

    控制器

    $subjects = Subject::lists('subject', 'id');
    

    查看

    {{ Form::select('subject', $subjects) }}
    

    【讨论】:

    • 我的问题出在表单请求中。如何在 'prepInput()' 中找到 id?
    • 我是 Laravel 的新手。你能解释一下吗?
    猜你喜欢
    • 1970-01-01
    • 2023-03-25
    • 1970-01-01
    • 2010-10-05
    • 1970-01-01
    • 2018-09-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多