Skip to content
Advertisement

Laravel – edit form updated checkbox value

I have a form created for editing a Trip. One field is ‘Public’ which is a boolean. When public is checked as true, I can’t get it to show and update on the edit form.

addTrip.blade.php

  <div class="form-group">
        {!! Form::label('is_public', 'Make Trip Itinerary Public') !!}
        {!! Form::checkbox('is_public', 'value'); !!}
</div>

editTrip.blade.php

<div class="form-group"> Make  Public
    <input type="checkbox" name="is_public" class="switch-input" value="{{$trip->is_public}}" />
    </div>

TripController

public function update(Request $request, $id)
{
    $this->validate($request, [
      'name'         => 'required',
      'email'        => 'required',
      'destination' => 'required',
      'startdate'   => 'required',
      'enddate'     => 'required',
      'user_id'     => 'required',

    ]);

    //Update Trip
    $trip = Trip::find($id);
    $trip->name = $request->input('name');
    $trip->email = $request->input('email');
    $trip->destination = $request->input('destination');
    $trip->startdate = $request->input('startdate');
    $trip->enddate = $request->input('enddate');
    $trip->is_public = $request->has('is_public');
    $trip->save();

    return redirect('trips')->with('sucess', 'Trip Updated');
}

Advertisement

Answer

input type checkbox has the property checked. if you want to make it checked then you have to add the property.

<input type="checkbox" name="is_public" class="switch-input" value="{{$trip->is_public}}" {{ $trip->is_public == 1 ? 'checked' : null }}/>

use whatever value you have stored in your database instead of 1.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement