dt1888 2015-01-28 17:11
浏览 39
已采纳

Laravel4:在非对象上调用成员函数update()

Following are my codes:

Model:

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'];

}

Controller Update Method:

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

Route:

Route::group(array('prefix' => 'admin'), function() {
    # Slides Management
    Route::resource('slides', 'AdminSlidesController', array('except' => array('show')));
});

Form in View:

{{ Form::model($slide, array('route' => 'admin.slides.update', $slide->id, 'method' => 'put')) }}
    @include('admin/slides/partials/form')
{{ Form::close() }}

Partial Form is simple form, not sure if I need to share it here or not. Let me know.

Error:

Edit page loads perfectly and populates data from db, but when I submit the edit form, I get following error:

Call to a member function update() on a non-object

The following line seems to be creating problems:

$slide->update($data);

I have searched over the internet for solution but nothing is working. Have tried composer dump_autoload, even tried doing everything from scratch in a new project, still same issue. :(

Help please!!

---- Edit ----

Just quickly tried following:

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

Now the error:

Creating default object from empty value

----- Solution: -----

The problem was with my form as suggested by @lukasgeiter I changed my form to following at it worked like a charm:

{{ Form::model($slide, array('route' => array('admin.slides.update', $slide->id), 'method' => 'put')) }}
  • 写回答

2条回答 默认 最新

  • dpj83664 2015-01-28 18:28
    关注

    The actual problem is not your controller but your form.
    It should be this instead:

    {{ Form::model($slide, array('route' => array('admin.slides.update', $slide->id), 'method' => 'put')) }}
    

    This mistake causes the controller to receive no id. Then find() yields no result and returns null.

    I recommend besides fixing the form you also use findOrFail() which will throw a ModelNotFoundException if no record is found.

    $slide = Slide::findOrFail($id);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了