60

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.


Here are my steps :

1 - Declare a route

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

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 - Create a button on my view

 {!! 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()!!}

4 - Test

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

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


Do I need include anything to use truncate() ?

Any hints on that will be much appreciated !

Bogdan
  • 43,166
  • 12
  • 128
  • 129
code-8
  • 54,650
  • 106
  • 352
  • 604

7 Answers7

88

the following should work as well,

Visitor::truncate();

nasirkhan
  • 9,948
  • 5
  • 27
  • 33
77

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();
Bogdan
  • 43,166
  • 12
  • 128
  • 129
47

From the Laravel Docs

https://laravel.com/docs/5.6/queries#deletes says:

If you wish to truncate the entire table, which will remove all rows and reset the auto-incrementing ID to zero, you may use the truncate method:

DB::table('users')->truncate();

Ryan
  • 22,332
  • 31
  • 176
  • 357
  • 1
    This solution is easier to use if your table doesn't have dedicated model class (link tables for example) – Simon PA Sep 03 '18 at 14:43
17

Laravel 8.0 docs

https://laravel.com/docs/8.x/queries#delete-statements

With query builder:

DB::table('users')->truncate();

And with model:

User::truncate();
Jsowa
  • 9,104
  • 5
  • 56
  • 60
6

another way if you dont have a model class for the table - Laravel 5.4

DB::connection(database connection name)->table(table name)->truncate();
itz_nsn
  • 648
  • 1
  • 8
  • 13
0

With this package you could also do it with a Laravel command:

https://github.com/laracraft-tech/laravel-useful-additions#dbtruncate

Install via composer:

composer require laracraft-tech/laravel-useful-additions

This is how it is working:

php artisan db:truncate users
Zacharias
  • 57
  • 9
0

No need to go with that lengthy process you just need to do as follows:-

Visitor::truncate(); 
  • Please note, there is an identical answer, from many years ago. Be sure to add some value, while answering a question that already has similar or identical solution – pierpy Apr 12 '23 at 20:16