Redadublex
Redadublex

Reputation: 129

Laravel send value from dropdown list to controller

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

Upvotes: 0

Views: 4876

Answers (1)

lukasgeiter
lukasgeiter

Reputation: 153120

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);

Upvotes: 2

Related Questions