php 在 Laravel 中编辑和更新数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30060915/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Edit and Updating Data in Laravel
提问by RodnovryJoshua
I am having some trouble editing data in Laravel. Here's the button to show Edit view:
我在 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()}}
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">×</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">×</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">×</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">×</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">×</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">×</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:
此视图不起作用,它无法将所选数据从$id
表单中获取。它正在返回此错误:
"Undefined property: Illuminate\Database\Eloquent\Collection::$id (View: C:\XAMPP\htdocs\ims2\app\views\modals\edit-spk.blade.php)"
“未定义的属性:Illuminate\Database\Eloquent\Collection::$id(视图:C:\XAMPP\htdocs\ims2\app\views\modals\edit-spk.blade.php)”
回答by Pawel Bieszczad
Can you dd($spk)
after $spk = Spk::find($id);
in the edit method and paste the output.
你可以在编辑方法dd($spk)
后$spk = Spk::find($id);
粘贴输出。
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.
试试这个,如果错误仍然存在,请告诉我。如果是这样,它是完全相同的错误吗?如果没有,请粘贴新的。
回答by abr
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
该$spk
变量是一个雄辩的集合,它在您的控制器上返回。您正在访问 $spk->id,这是不正确的,需要访问集合中的单个元素才能对其进行编辑
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
SpkContent 表单请求
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
至少,通过这种方式,您可以将验证与操作分开,代码更加简洁和可维护