Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Truncate a table in Laravel 5

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 !

like image 265
code-8 Avatar asked Oct 10 '15 13:10

code-8


People also ask

What is truncate () in laravel?

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();

How do you empty a table in laravel?

Our records will be automatically deleted from the cache if users delete all the records from the table. But we can delete it from the cache only if we are using the model of Laravel Eloquent. In the first example, we are going to use the truncate() function, which is used to delete all the records.

Is it possible to truncate a table in the DBMS app?

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.

Is it possible to truncate tables referenced by foreign keys?

As the error says, you can not truncate tables referenced by foreign keys. Delete should work though... Show activity on this post. Show activity on this post. To clear a table using Eloquent:

What is the use of truncate method in query builder?

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: Show activity on this post. Show activity on this post. Show activity on this post. Thanks for contributing an answer to Stack Overflow!


2 Answers

the following should work as well,

Visitor::truncate();

like image 71
nasirkhan Avatar answered Oct 14 '22 16:10

nasirkhan


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(); 
like image 28
Bogdan Avatar answered Oct 14 '22 16:10

Bogdan