Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel set radio button is checked based on database value

I need to show the radio button checked based on database value and update the value if i change the selection of radio button and showed on form.

 <label class="radio-inline">
                                <input type="radio"  id="option1" name="status" value="{{ old('is_active', $site->is_active) }}" name="status"  >OFF</label>
                                <label class="radio-inline">
                                <input type="radio" id="option2" name="status" value="{{ old('is_active', $site->is_active) }}" name="status">ON</label>

this is inside my controller

 $site->update([
            'name'=>$request['name'],
            'copyright'=> $request['copyright'],
            'is_active'=>$request['status'] == 'true' ? 1 : 0,
            'message'=>$request['message'],
            'datatime'=>date('Y-m-d', strtotime($request['datatime'])),
            'url'=>$request['url'],
            'metadata'=>$request['metadata']

        ]);
like image 732
Karthiga Avatar asked Apr 11 '17 12:04

Karthiga


1 Answers

1) NO need two name attribute.

2) you can use {{ ($site->is_active=="0")? "checked" : "" }} to checked based on database value .

<input type="radio"  id="option1" name="status" value="0"  {{ ($site->is_active=="0")? "checked" : "" }} >OFF</label>

<input type="radio" id="option2" name="status" value="1" {{ ($site->is_active=="1")? "checked" : "" }} >ON</label>

3) for select box do like this

<selec name="xx" >
<option value="1" {{ ($site->select_values=="1")? "selected" : "" }} ></option>
 .
 .
 </select>

Controller :

$site->update([
        'name'=>$request['name'],
        'copyright'=> $request['copyright'],
        'is_active'=>$request['status'], // == 'true' ? 1 : 0
        'message'=>$request['message'],
        'datatime'=>date('Y-m-d', strtotime($request['datatime'])),
        'url'=>$request['url'],
        'metadata'=>$request['metadata']

    ]);
like image 143
JYoThI Avatar answered Sep 20 '22 05:09

JYoThI