ehcall
ehcall

Reputation: 25

Single Checkbox Form Laravel 5.5

I'm working on a webpage where users can click a checkbox; any user can click the checkbox, and the result will be the same when viewed by all users. So, if User A checks the box, User B will see it as checked. If User B then unchecks it, User A will see it as unchecked.

I can get the checkbox to display the value from the database, but I can't seem to get the value to change and update in the database when a user clicks on the checkbox.

My blade layout looks like this:

<form method="POST" action="{{ action('QuestionController@answered', $question->id) }}"  role="form">
     {{ csrf_field() }}
     <input type="checkbox" id="answered" name="answered" value="0" @if($question->answered == 1) checked @endif> Answered
</form>

The function in my controller looks like this:

public function answered(Request $request, $id)
    {

        $request->request->add(['answered_userid' => $this->userId]);
        $this->validate($request, [
             'answered' => 'required',
             'answered_userid' => 'required'

        ]);
        Question::find($id)->update($request->all());
    }
}

And, in my routes file, I have this:

Route::post('questions/answered', ['as' => 'questions.answered', 'uses' => 'QuestionController@answered']);

EDIT: I've updated it to include the name, but I'm still running into the same problem.

Upvotes: 1

Views: 1447

Answers (2)

Mahdi Younesi
Mahdi Younesi

Reputation: 7509

Set the name and its value

<input type="checkbox" name='answered' {{$question->answred ==1 ? Value="1" checked : value="0"}}>

Upvotes: 0

Laerte
Laerte

Reputation: 7083

You have to set a name in the input. The request will use the name of the input as the parameter:

<input type="checkbox" name="answered" id="answered" value="0" @if($question->answered == 1) checked @endif> 

Upvotes: 0

Related Questions