以下是我的代码:
模型:
class Slide extends \Eloquent {
// Add your validation rules here
public static $rules = [
'title' => 'required|between:3,100',
'image' => 'required',
'url' => 'url',
'active' => 'integer'
];
// Don't forget to fill this array
protected $fillable = ['title', 'image', 'url', 'active'];
}
Run Code Online (Sandbox Code Playgroud)
控制器更新方法:
public function update($id)
{
$slide = Slide::find($id);
$validator = Validator::make($data = Input::all(), Slide::$rules);
if ($validator->fails())
{
return Redirect::back()->withErrors($validator)->withInput();
}
$slide->update($data);
return Redirect::route('admin.slides.index')
->with('message', 'Slide has been updated.')
->with('message-type', 'alert-success');
}
Run Code Online (Sandbox Code Playgroud)
路线:
Route::group(array('prefix' => 'admin'), function() {
# Slides Management
Route::resource('slides', 'AdminSlidesController', array('except' => array('show')));
});
Run Code Online (Sandbox Code Playgroud)
表单视图:
{{ Form::model($slide, array('route' => 'admin.slides.update', $slide->id, 'method' => 'put')) }}
@include('admin/slides/partials/form')
{{ Form::close() }}
Run Code Online (Sandbox Code Playgroud)
部分表格是简单表格,不确定是否需要在此处共享。让我知道。
错误:
编辑页面可以完美加载并填充数据库中的数据,但是当我提交编辑表单时,出现以下错误:
在非对象上调用成员函数update()
以下行似乎在造成问题:
$ slide-> update($ data);
我已经在互联网上搜索解决方案,但是没有任何反应。尝试过作曲家dump_autoload,甚至尝试在新项目中从头开始做所有事情,仍然是同样的问题。:(
请帮忙!!
----编辑----
只需快速尝试以下操作:
public function update($id)
{
$slide = Slide::find($id);
$slide->title = Input::get('title');
$slide->save();
return Redirect::route('admin.slides.index')
->with('message', 'Slide has been updated.')
->with('message-type', 'alert-success');
}
Run Code Online (Sandbox Code Playgroud)
现在出现错误:
从空值创建默认对象
问题出在@lukasgeiter所建议的表单上,我将表单更改为跟随它,就像魅力一样:
{{ Form::model($slide, array('route' => array('admin.slides.update', $slide->id), 'method' => 'put')) }}
Run Code Online (Sandbox Code Playgroud)
用$slide->save();代替$slide->update($data);
要更新模型,您可以检索它,更改属性并使用save方法:
例如:
$user = User::find(1);
$user->email = 'john@foo.com';
$user->save();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3384 次 |
| 最近记录: |