0

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

1 Answer 1

2

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);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.