Skip to content

Commit

Permalink
Add the method that actually makes a question
Browse files Browse the repository at this point in the history
  • Loading branch information
ysbrandB committed Jun 26, 2024
1 parent d3529e0 commit df95342
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions app/Http/Controllers/QuestionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@ public function create()

public function store(Request $request)
{
$question = Question::create($request->only('title', 'description'));
foreach ($request->input('answers')??[] as $newAnswer) {
$answer = $question->answers()->updateOrCreate(['id' => $newAnswer['id'] ?? null], ['text' => $newAnswer['text']]);
$answer->attributes()->sync(collect($newAnswer['attributes'])->pluck('id'));
}

return to_route('questions.index')->with('success', 'Question saved!');
}

public function edit($id)
Expand All @@ -41,12 +48,12 @@ public function update(Request $request, $id)
{
$question = Question::findOrFail($id);
$question->update($request->only('text', 'description'));
foreach ($request->input('answers') as $newAnswer) {
foreach ($request->input('answers')??[] as $newAnswer) {
$answer = $question->answers()->updateOrCreate(['id' => $newAnswer['id'] ?? null], ['text' => $newAnswer['text']]);
$answer->attributes()->sync(collect($newAnswer['attributes'])->pluck('id'));
}

return to_route('questions.edit', $question->id)->with('success', 'Question saved!');
return to_route('questions.index')->with('success', 'Question saved!');
}

public function destroy($id)
Expand Down

0 comments on commit df95342

Please sign in to comment.