Laravel将值从下拉列表发送到控制器
问题描述:
I have the following code in my view page
<form action="{{ action('AnswerController@handleCreate') }}" method="post" role="form">
<div class="form-group dropdown">
<label for="question">Question</label>
<select id="question" class="drop" name="question">
@foreach($questions as $question)
<option value="{{$question->question}}">{{$question->question}}</option>
@endforeach
</select>
</div>
in my controller i have the following code
$question = Question::whereQuestion(Input::get('question'))->first();
$n = $question->id;
They give me an error at $n=$question ->id telling trying to get property of non-object
我在视图页面中有以下代码 p>
&lt ; form action =“{{action('AnswerController @ handleCreate')}}”method =“post”role =“form”&gt;
&lt; div class =“form-group dropdown”&gt;
&lt; label for =“question”&gt;问题&lt; / label&gt;
&lt; select id =“question”class =“drop”name =“question”&gt;
@foreach($ questions as $ question)
&lt ; option value =“{{$ question-&gt; question}}”&gt; {{$ question-&gt; question}}&lt; / option&gt;
@endforeach
&lt; / select&gt;
&lt; / div&gt;
code> pre>
在我的控制器中我有以下代码 p>
$ question = Question :: whereQuestion( 输入:: get('问题')) - &gt; first();
$ n = $ question-&gt; id;
code> pre>
他们给出 我错误$ n = $ question - &gt; id告诉我试图获取非对象的属性 p>
div>
答
Instead of using the question text as value and query the id it afterwards why don't you just set the id as value from the beginning?
<select id="question" class="drop" name="question">
@foreach($questions as $question)
<option value="{{$question->id}}">{{$question->question}}</option>
@endforeach
</select>
And then you can just do Input::get('question')
and you have the id of the selected question. If you then wanted to get the full model:
$questionId = Input::get('question');
$question = Question::find($questionId);