i'm creating a simple question-and-answer web app using Laravel, and i'm new to this,
So there's a USER who posts a QUESTION and letting someone to ANSWER the question, my progress so far, I can register as a USER, post a QUESTION, BUT i'm having trouble saving the ANSWER, here's my code:
{!! Form::open(['action' => 'AnswersController@store', 'method' => 'POST'])!!}
<div class="from-group">
{{Form::label('answer', 'Answer')}}
{{Form::textarea('answer', '', ['class' => 'form-control', 'placeholder' => 'type your answer here'])}}
<br>
{{Form::submit('Submit Answer', ['class' => 'btn btn-primary'])}}
</div>
{!! Form::close()!!}
and here's a Screenshot of it:
UI Screenshot
Now i want to insert the answer to my database, the table structure is: database table structure
and here is my Store function from my controller:
public function store(Request $request)
{
$this->validate($request, [
'answer' => 'required'
]);
$answer = new Answer;
$answer->answer = $request->input('answer');
$answer->question_id = $question_id;
$answer->user_id = auth()->user()->id;
$answer->save();
return redirect('/answer/$question_id')->with('success', 'Answer Posted');
}
Now, My biggest problem is, how can I get the question ID? Every time I run this, it's giving me errors:
Trying to get property of non-object
And i think its referring to the "question_id"