Martney Acha
Martney Acha

Reputation: 3002

Radio button in my laravel 5.4 cannot be read by request

All of the other request in my view is included already in the request using dd($request), but my radio button input is cannot be read by request. attached here is my code

View

<form method="post" action="{{route('update-dependent')}}">
   <div class="form-group">
       {{csrf_field()}}
   <div class="form-group">
    <label>Last Name</label>
    <input type="text" name="lname" value="{{$dependent->lname}}" class="form-control">
    </div>                                              

        <div class="form-group">
        {{-- conditional statement to check what geneder --}}
        <input type="hidden" name="gender" value="bading" class="form-control">
        <label>Male</label>     
        <input type="radio" name="gender" value="male" class="form-control">
        <label>Female</label>
        <input type="radio" name="gender" value="female" class="form-control">
        </div>      

        <div class="form-group">
        <label>Relationship</label>
        <input type="text" name="relationship" value="{{$dependent->relationship}}" class="form-control">
        </div>  

        <div class="form-group">
        <button type="submit" class="btn btn-primary">Update</button>
        </div>

</form>

Controller

   HMO::UpdateDependent($request); 

   return redirect()->route('edit', request('hmo_id'));

Model

public static function updateDependent($request)
{
    DB::table('hmos_detail')
    ->where('id', $request->get('dependent_id'))
    ->update([  'lname'         => $request->get('lname'),
                'gender'        => $request->get('gender'),
                'relationship'  => $request->get('relationship'),
    return true;
}

result

 array:2 [▼
      "_token" => "sP9FVEbL3pI5Yaf2jlSfDATNtOTSz2Rheb4bH9Ti"
      "lname" => "test"
      "relationship" => "WIFE"
    ]

Upvotes: 0

Views: 1430

Answers (3)

Gammer
Gammer

Reputation: 5608

As @Sree mentioned, You need to give gender to the fillable.

protected $fillable = ['gender', ....];

As from the documentation :

Mass Assignment You may also use the create method to save a new model in a single line. The inserted model instance will be returned to you from the method. However, before doing so, you will need to specify either a fillable or guarded attribute on the model, as all Eloquent models protect against mass-assignment by default.

Upvotes: 0

Sreejith BS
Sreejith BS

Reputation: 1203

It might be because you forgot to add gender in your Mass Assignment. In your model add,

protected $fillable = ['gender', ....];

Upvotes: -1

usrNotFound
usrNotFound

Reputation: 2800

First of all if you want value of radio then make sure you have proper validation in place.

If you don't know how to validate then have a look at following link https://laravel.com/docs/5.5/validation#validation-quickstart

Furthermore If you want some default value for radio button then make sure you have a hidden field that will have default value is this case you dont need validation.

So the basic example would be

<input type="hidden" name="gender" value="bading" class="form-control">

The radio btn will always have bading as default weather its selected or not. But If you add

<input type="radio" name="gender" value="male" class="form-control">
<input type="radio" name="gender" value="female" class="form-control">

Then the hidden field is replaced with the selected value.

Upvotes: 2

Related Questions