在Laravel 5中截断一张桌子 [英] Truncate a table in Laravel 5

查看:97
本文介绍了在Laravel 5中截断一张桌子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

描述:我有一张表满满测试的数据。有时,我想清除新的数据。我可以在DBMS应用程序中执行截断,如 MySQL WorkBench ,但我正在尝试在我的应用程序中实现。

Description : I have a table full of tested data. Sometimes, I want to clear it out for new data. I can perform the truncate in the DBMS App like MySQL WorkBench, but I'm trying to achieve it within my application instead.

目标:当点击时,使按钮截断数据库中的表。

Goal : to make a button to truncate a table in a database when on click.

以下是我的步骤:

1 - 声明路线

Route::delete('visitor/truncate',array('as'=>'visitor.truncate', 'uses'=>'VisitorController@truncate'));






2 - 创建一个 truncate 函数在我的 VisitorController


2 - Create a truncate function in my VisitorController

public function truncate()
{

    $visitors = Visitor::all();
    $visitors ->truncate();

    return View::make('visitors.index')
        ->with('success', 'Truncate Done');
}






3 - 在我的视图上创建一个按钮

 {!! Form::model($visitors, array( 'route' => array('visitor.truncate'),'method' => 'DELETE')) !!}
          <button type="submit"  class="btn bgm-red btn-float waves-effect waves-effect waves-button waves-float"><i class="md md-remove"></i></button>
      {!! Form::close()!!}






strong> 4 - 测试

当我点击它,它进入我的 truncate()功能在我的控制器,但我不断得到这个错误

When I click on it, it get into my truncate() function in my controller, but I keep getting this error


调用未定义的方法Illuminate\Database\Eloquent\Collection :: truncate ()

Call to undefined method Illuminate\Database\Eloquent\Collection::truncate()






我需要包含任何使用 truncate ()

任何提示都将不胜感激!

Any hints on that will be much appreciated !

推荐答案

truncate 方法是Query Builder的一部分。但是 Visitor :: all()返回一个集合实例。您需要使用以下内容构建查询:

The truncate method is part of the Query Builder. However Visitor::all() returns a Collection instance. You need to build the query using the following:

Visitor::query()->truncate();

这篇关于在Laravel 5中截断一张桌子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆