在Laravel中编辑和更新数据

时间:2015-05-05 18:48:19

标签: php laravel edit crud

我在Laravel中编辑数据时遇到了一些麻烦。这是显示编辑视图的按钮:

{{ 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()}}                        

这是路线:

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

这是控制器:

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');
  }
}

这是用于更新数据的视图:

@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

此视图不起作用,无法将$id中的选定数据导入表单。它返回了这个错误:

  

“Undefined property:Illuminate \ Database \ Eloquent \ Collection :: $ id(查看:C:\ XAMPP \ htdocs \ ims2 \ app \ views \ modals \ edit-spk.blade.php)”

2 个答案:

答案 0 :(得分:0)

您可以在编辑方法dd($spk)之后$spk = Spk::find($id);粘贴输出。

编辑:

按钮应如下所示(无形式

<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>

控制器:

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

试试这个,如果错误仍然存​​在,请告诉我。如果是这样,它是否是同样的错误?如果没有,请粘贴新的。

答案 1 :(得分:0)

关于错误:

您认为您已经获得了这段代码

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

$spk变量是一个雄辩的集合,它在控制器上返回。您正在访问$ spk-> id,这是错误的,需要访问集合中的单个元素才能对其进行编辑

同时让我简化您的代码:

在路由中,您指定控制器中的哪种方法将处理请求。 在控制台中编写:

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;
    }
}

至少,通过这种方式,您可以将验证与操作,更简洁,可维护的代码分开