在恢复软删除时调用未定义的方法stdClass :: restore()

时间:2016-01-10 02:53:38

标签: php laravel-5

我尝试恢复软删除的数据,但我只是不断收到此错误 Call to undefined method stdClass::restore()

我的路线看起来像这样

    Route::resource('/dvd', 'DvdController');
    Route:get('dvd/{id}/restore', 'DvdController@restore');

这是我的控制器

    public function restore($id)
{
    //Dvd::withTrashed()->where('id','=',Input::get('id'))->restore();
    //Dvd::find($id)->restore();
    $dvds = DB::table('dvds')->where('id', $id)->first();
    $dvds->restore($id);
    $view = redirect('dvd')->with('message', 'Data berhasil di-restore');
    return $view;
}

我从按钮

调用该方法
    <a style="margin-bottom: 5px;" class="btn btn-small btn-primary btn-block" 
    href="{{ URL('dvd/' . $data->id . '/restore') }}">Restore</a>

我不知道自己错了什么,我是PHP和laravel的新手,请包含正确的代码。 谢谢

1 个答案:

答案 0 :(得分:2)

您正在使用数据库外观,因此您获得了一个StdClass实例,而不是一个Eloquent模型,而restore()是一个Eloquent类的方法。

您需要更改:

$dvds = DB::table('dvds')->where('id', $id)->first();

类似于:

$dvds = Dvd::withTrashed()->where('id', $id)->first();

然后它应该可以正常工作。

相关问题