php – Edit and Updating Data in Laravel

I am having some trouble editing data in Laravel. Here’s the button to show Edit view:

{{ Form::open(array('route' => array('edit_spk', 'id'=> $spk_data->id), 'method' => 'GET', 'style' => 'display:inline')) }}
<button class="btn btn-success btn-line btn-rect">
  <i class="icon-pencil icon-white"></i> Edit
</button>
{{Form::close()}}                        

Here’s the route:

Route::get('spk/edit/{id}', array('as'=>'edit_spk','uses'=>'SpkController@edit'));
Route::put('spk/update/{id}', array('as'=>'update_spk','uses'=>'SpkController@update'));

Here’s the controller:

public function edit($id){
  $spk = Spk::find($id);
  $spk->distribution_code=Input::get('distribution_code');
  $spk->destination=Input::get('destination');
  $spk->hlr=Input::get('hlr');
  $spk->first_iccid=Input::get('first_iccid');
  $spk->last_iccid=Input::get('last_iccid');
  $spk->quantity=Input::get('quantity');
  return View::make('modals.edit-spk', compact('spk'));
}

public function update($id) {
  $rules = array(
    'distribution_code' => 'required',
    'destination' => 'required',
    'hlr'=> 'required',
    'first_iccid' => 'required',
    'last_iccid' => 'required',
    'quantity' => 'required'
  );

  $validator = Validator::make(Input::all(), $rules);

  if ($validator->fails()) {
    return Redirect::to('modals.edit-spk')->withErrors($validator);
  } else {
    // store
    $update = Spk::find($id);
    $update->distribution_code=Input::get('distribution_code');
    $update->destination=Input::get('destination');
    $update->hlr=Input::get('hlr');
    $update->first_iccid=Input::get('first_iccid');
    $update->last_iccid=Input::get('last_iccid');
    $update->quantity=Input::get('quantity');
    $update->save();

    // redirect
    Session::flash('message', 'Successfully updated SPK !');
    return Redirect::to('spk_view');
  }
}

And here’s the View for update the data :

@extends('dashboard.dashboard')
@section('content')
@if (Session::has('message'))
<div class="alert alert-info">{{ Session::get('message') }}</div>
@endif
<h4> {{link_to('dashboard','Home');}} > {{link_to('spk_view','SPK');}} > Update SPK</h4><hr></hr>
<div class="panel panel-default">
    <div class="panel-heading">
        Update SPK
    </div>
    <div class="panel-body">
        {{ Form::model($spk,array('method' => 'PUT', 'class'=>'form-horizontal','route'=>array('update_spk', $spk->id))) }}
        <div class="form-group">
            <label class="control-label col-lg-2">Distribution Code</label>
            <div class="col-lg-4">
                <div class="input-group">
                    <input class="form-control" name ="distribution_code" type="text" data-mask="M99/99/99/9999" />
                    <span class="input-group-addon">M99/99/99/9999</span>
                </div>
            </div>
            <div class="col-lg-5">
                {{ $errors->first('distribution_code',
                '<div class="alert alert-danger alert-dismissable">
                <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
                Anda belum mengisi data dengan benar !
            </div>
            ')  }}
        </div>
    </div>
    <div class="form-group">
        <label class="control-label col-lg-2">Destination</label>
        <div class="col-lg-3">
            <input type="text" name="destination" class="form-control" />
        </div>
        <div class="col-lg-4">
            {{ $errors->first('destination',
            '<div class="alert alert-danger alert-dismissable">
            <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
            Anda belum mengisi data dengan benar !
        </div>
        ')  }}
    </div>
</div>

<div class="form-group">
    <label class="control-label col-lg-2">HLR</label>
    <div class="col-lg-3">
        <input type="text" id="hlr" name="hlr" class="form-control" />
    </div>
    <div class="col-lg-4">
        {{ $errors->first('hlr', 
        '<div class="alert alert-danger alert-dismissable">
        <button type="button" class="close" data-dismiss="alert"aria-hidden="true">&times;</button>
        Anda belum mengisi data dengan benar !
    </div>
    ')  }}
</div>
</div>
<div class="form-group">
    <label class="control-label col-lg-2">First ICCID</label>
    <div class="col-lg-3">
        <input type="text" id="first_iccid" name="first_iccid" class="form-control" />
    </div>
    <div class="col-lg-4">
        {{ $errors->first('first_iccid', 
        '<div class="alert alert-danger alert-dismissable">
        <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
        Anda belum mengisi data dengan benar !
    </div>
    ')  }}
</div>
</div>
<div class="form-group">
    <label class="control-label col-lg-2">Last ICCID</label>
    <div class="col-lg-3">
        <input type="text" id="last_iccid" name="last_iccid" class="form-control" />
    </div>
    <div class="col-lg-4">
        {{ $errors->first('last_iccid',
        '<div class="alert alert-danger alert-dismissable">
        <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
        Anda belum mengisi data dengan benar !
    </div>
    ')  }}
</div>
</div>
<div class="form-group">
    <label class="control-label col-lg-2">Quantity</label>
    <div class="col-lg-3">
        <input type="text" id="quantity" name="quantity" class="form-control" />
    </div>
    <div class="col-lg-4">
        {{ $errors->first('quantity', 
        '<div class="alert alert-danger alert-dismissable">
        <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
        Anda belum mengisi data dengan benar !
    </div>
    ')  }}
</div>
</div>
<div class="form-actions no-margin-bottom" style="text-align:center;">
    {{ Form::submit('Update SPK', array('class' => 'btn btn-primary btn-line btn-rect')) }} 
</div>
{{Form::close()}}
</div>
</div>
@stop

This view doesn’t work and it can’t get the selected data from $id into the form. It is returning this error:

“Undefined property: IlluminateDatabaseEloquentCollection::$id (View: C:XAMPPhtdocsims2appviewsmodalsedit-spk.blade.php)”

Leave a Comment