Truncate a table in Laravel 5
Stefan Izdrail
Founder & Senior Architect · 2026-06-29
Title: Truncate a Table in Laravel 5: A Comprehensive Guide with Code Examples
Introduction: Managing data efficiently is crucial for any application. Sometimes, you may need to clear out an existing table of tested data to make room for fresh data. While it's possible to perform this task from the DBMS App like MySQL WorkBench, in this blog post, we will discuss how to achieve the same functionality within your Laravel application.
Goal: The goal is to create a button that, when clicked, would execute a database query to truncate a table containing visitors' records.
The Steps:
1. Declare a Route for Truncating the Table: First, define a route in your routes file using Laravel's built-in HTTP Verb methods. We need to specify the HTTP method (DELETE) and the URL path ('visitor/truncate'). This will map requests matching that URL and send them to our controller function.
Route::delete('visitor/truncate',array('as'=>'visitor.truncate', 'uses'=>'VisitorController@truncate'));
2. Create the 'truncate' Function: As per our goal, we need to write a function in the VisitorController class that handles this request. Here, we will retrieve all visitors from our database and truncate them using the related Eloquent model function (Visitor::all()->truncate()). After executing the truncation operation, return a view with a success message.
public function truncate()
{
$visitors = Visitor::all();
$visitors ->truncate();
return View::make('visitors.index')
->with('success', 'Truncate Done');
}
3. Add the Truncation Button to the View: To make this feature accessible, add a button in your view that will send requests with the DELETE method to the route we declared earlier. Use Laravel's Form::model() helper for managing form fields as shown below.
{!! 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 the Application: Finally, test your application by clicking on the button. If everything is set up correctly, a request will be sent to our controller's truncate function, which should truncate the table as expected.
Troubleshooting: In case of errors like "Call to undefined method Illuminate\Database\Eloquent\Collection::truncate()", double-check your code for any typo or misplaced model methods. Ensure you have properly imported and used the Eloquent model (in this case, it should be using 'use App\Models\Visitor' at the top of your controller class).
Conclusion: Truncating a database table within your Laravel application can be achieved by declaring a route, creating a relevant function in your controller, and adding a button to your view. This process allows you to efficiently manage data and ensure your application stays up-to-date with the latest information. By following these steps, you'll have a clearer understanding of how to achieve functionalities within Laravel and optimize your database operations.