Reputation: 121
I have two fields in my database 'administrator' and 'moderator' type radio button and I would like:
Save the value 1 when I chose the administrator and the value 0 for the moderator
and when I chose the moderator I would like to save the value 1 and the value 0 for the adminitrator
I try with this code but it doesn't work.
my view :
<label class="mt-checkbox">
<input type="radio" id="administrator" name="radio" value="administrator"> Administrator
</label>
<label class="mt-checkbox">
<input type="radio" id="moderator" name="radio" value="moderator"> Moderator
</label>
My Controller :
public function add_profil(Request $request)
{
$profil = new Profil();
if($request->adminitrator == 'checked'){
$profil->adminitrator = 1;
$profil->moderator = 0;
}
elseif ($request->moderator == 'checked') {
$profil->moderator = 1;
$profil->adminitrator = 0;
}
$profil->save();
}
Upvotes: 0
Views: 14423
Reputation: 1494
When the request has been sent, you get inputs with their names. so your radio buttons have the name 'radio'. So you should check the request with that input name, not the value of the input.
I recommend you fetch the requests with request->all() method and set it to a variable $request. This way you can treat request like an array and access the request values with the key that is same as the name of the input.
So you can do it this way: first, in your controller, you must check what is the request value with the name of radio and then if it has the value of administrator you put administrator value of one and then the moderator value of 0. And on the other side in else statement you should do it otherwise like this code below:
public function add_profil(Request $request)
{
$request = request->all();
$profil = new Profil();
if($request['radio'] == 'administrator'){
$profil->adminitrator = 1;
$profil->moderator = 0;
}
elseif ($request['radio'] == 'moderator') {
$profil->moderator = 1;
$profil->adminitrator = 0;
}
$profil->save();
}
Note that you type the administrator wrong in your if statement. you misstype 's'.
Upvotes: 1
Reputation: 1537
You're getting wrong name in laravel, change form to this:
public function add_profil(Request $request)
{
$profil = new Profil();
if($request->administrator == 'checked'){
$profil->adminitrator = 1;
$profil->moderator = 0;
}
elseif ($request->moderator == 'checked') {
$profil->moderator = 1;
$profil->adminitrator = 0;
}
$profil->save();
}
<label class="mt-checkbox">
<input type="radio" id="administrator" name="administrator" value="checked"> Administrator
</label>
<label class="mt-checkbox">
<input type="radio" id="moderator" name="moderator" value="checked"> Moderator
</label>
Upvotes: -1
Reputation: 182
Your request has <input name='radio'>
so you'll want to find the request with that, like so:
public function add_profil(Request $request)
{
$profil = new Profil();
if($request->radio == 'adminitrator'){
$profil->adminitrator = 1;
$profil->moderator = 0;
}
elseif ($request->radio == 'moderator') {
$profil->moderator = 1;
$profil->adminitrator = 0;
}
$profil->save();
}
Upvotes: 1