4

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: Illuminate\Database\Eloquent\Collection::$id (View: C:\XAMPP\htdocs\ims2\app\views\modals\edit-spk.blade.php)"

Tim Lewis
  • 27,813
  • 13
  • 73
  • 102
RodnovryJoshua
  • 73
  • 1
  • 2
  • 9
  • array(9) { ["id"]=> int(35) ["distribution_code"]=> string(14) "M04/20/01/2014" ["destination"]=> string(6) "Jaksel" ["hlr"]=> string(3) "114" ["first_iccid"]=> string(3) "123" ["last_iccid"]=> string(3) "456" ["quantity"]=> string(3) "200" ["created_at"]=> string(19) "2015-05-03 07:25:59" ["updated_at"]=> string(19) "2015-05-03 07:25:59" } – RodnovryJoshua May 05 '15 at 20:22
  • it could be the `compact('spk')`, try `'route'=>array('update_spk', $spk[0]->id)`, if that doesnt work please `{{dd($spk)}}` before the Form in view and paste the whole output. – Pawel Bieszczad May 05 '15 at 20:41
  • The result still same : array(9) { ["id"]=> int(35) ["distribution_code"]=> string(14) "M04/20/01/2014" ["destination"]=> string(6) "Jaksel" ["hlr"]=> string(3) "114" ["first_iccid"]=> string(3) "123" ["last_iccid"]=> string(3) "456" ["quantity"]=> string(3) "200" ["created_at"]=> string(19) "2015-05-03 07:25:59" ["updated_at"]=> string(19) "2015-05-03 07:25:59" } But, it doesnt appear in my Update form. – RodnovryJoshua May 05 '15 at 20:48
  • if you change `route'=>array('update_spk', $spk->id)` to `'route'=>array('update_spk', $spk['id'])` do you still get the error? or is the form showing up but its empty? – Pawel Bieszczad May 05 '15 at 20:52
  • I've changed the button and the controller, and the result is : "Undefined index: id (View: C:\XAMPP\htdocs\ims2\app\views\modals\edit-spk.blade.php)" – RodnovryJoshua May 06 '15 at 04:33
  • Here's the result for dd : array(9) { ["id"]=> int(35) ["distribution_code"]=> string(14) "M04/20/01/2014" ["destination"]=> string(6) "Jaksel" ["hlr"]=> string(3) "114" ["first_iccid"]=> string(3) "123" ["last_iccid"]=> string(3) "456" ["quantity"]=> string(3) "200" ["created_at"]=> string(19) "2015-05-03 07:25:59" ["updated_at"]=> string(19) "2015-05-03 07:25:59" } – RodnovryJoshua May 06 '15 at 04:35
  • Did you change the route parameter on the form back to what you had originally? If not please do so. – Pawel Bieszczad May 06 '15 at 04:45

2 Answers2

0

Can you dd($spk) after $spk = Spk::find($id); in the edit method and paste the output.

Edit:

The button should look like this (no form)

<a href="{{ route('edit_spk', ['id' => $spk_data->id]) }}" class="btn btn-success btn-line btn-rect">
    <i class="icon-pencil icon-white"></i> Edit
</a>

The controller:

public function edit($id){
    $spk = Spk::find($id);
    return View::make('modals.edit-spk', [
        'spk' => $spk
    ]);
}  

Try this and let me know if the error persists. If so, is it the same exact error? If not, please paste the new one.

Pawel Bieszczad
  • 12,925
  • 3
  • 37
  • 40
0

About the error:

In your view, you've got this piece of code

{{ Form::open(array('route' => array('edit_spk', 'id'=> $spk_data->id), 'method' => 'GET', 'style' => 'display:inline')) }}

the $spk variable is an eloquent collection, that is returned on your controller. You're accessing $spk->id, which is incorrect, need to access the a single element in the collection in order to edit it

Allow me to simplify your code in the meanwhile:

In your route, you specify what method in the controller will handle the request. Write in your console:

php artisan make:request SpkContent

SpkController

use App\Http\Requests\SpkContent;

public function edit($id, SpkContent $request){

  $spk = Spk::find($id);
  //Not sure what this is for really, but you can simplify the lines below with this one
  //$data = $request->validated();
  //$spk = array_merge($spk->toArray(), $data);
  $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, SpkContent $request) {
    // store
    $data = $request->validated();
    Spk::findOrFail($id)->update($data);

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

SpkContent FormRequest

class SpkContent extends FormRequest
{
    public function authorize()
    {
        return true;
    }

    public function rules()
    {
        //Add whatever validations you need, as in: max size, integers, etc
        $content = [
         'distribution_code' => 'required',
         'destination' => 'required',
         'hlr'=> 'required',
         'first_iccid' => 'required',
         'last_iccid' => 'required',
         'quantity' => 'required'
        ];
        return $content;
    }
}

At least, this way you can split validation from operation, much cleaner and maintainable code

abr
  • 2,071
  • 22
  • 38