How to change Laravel model's table name
Stefan Izdrail
Founder & Senior Architect · 2026-06-29
Title: Tackling Model Table Name Changes in Laravel
Your Laravel application has grown over time, and you have recently renamed a database table's name from "domain_related_settings" to "DomainRelatedSettings". However, when referencing this model through the corresponding DomainRelatedSetting class, your code still uses the old table name in queries. This post will guide you on how to ensure that the new table name is used consistently throughout your Laravel application.
First and foremost, as mentioned in your earlier response, rolling back all migrations and changing the specific migration responsible for creating the table are essential steps to reflect the new table name in your database. Let us walk through the process to ensure the proper execution of these changes:
1. Rollback all the existing migrations: If you're using Laravel 5, use the command `php artisan migrate:rollback`. This rolls back all migrations and starts with a clean slate in your database.
2. Modify the specific migration file for the DomainRelatedSettings model: Update the table name to "DomainRelatedSettings" within the appropriate migration file (assuming it's named `xxx_create_domainrelatedsettings_table`). Replace any references of the old table name with the new one.
3. Migrate your database again: Run the command `php artisan migrate` to create the tables and relationships in your database, now using the new table name. To double-check that all migrations have been applied successfully, run `php artisan migrate:status`.
Now that you have performed all required changes in your database, it is time to update the DomainRelatedSetting model class to point to the new table name. Follow these steps:
1. Open the DomainRelatedSetting class file (e.g., app/models/DomainRelatedSetting.php).
2. Replace any references to the old table name in the class with the new one, "DomainRelatedSettings". This should include the model's namespace and table name constants if you have defined them. If not, ensure to define a constant for your new table name within this file.
3. Double-check your code to verify that all references to the old table name are replaced with the new one. Be vigilant and make sure there are no typos or other errors that may break your application's functionality.
4. Test your application to ensure that the new table name is being used correctly in queries, relationships, and other aspects of your code. If you encounter any issues, troubleshoot them accordingly.
Remember that Laravel's eloquent models use the table names defined in their corresponding migrations for all database-related tasks. Therefore, maintaining consistency across all aspects of your application is essential to ensure that queries and other operations work as expected once your migrations are committed.
To conclude this blog post, you must perform thorough testing on your Laravel application after these changes have been implemented. This guarantees that the new table name is used consistently in all queries, relationships, and other aspects of your codebase. Employing best practices when working with database migrations will save you time in the long run and prevent unexpected issues from arising.
In summary: When changing Laravel model's table name, roll back all existing migrations, modify the corresponding migration file, apply new migrations, update the DomainRelatedSetting model class to use the new table name, test your application, and ensure consistent usage of the new table name throughout your codebase. By following these steps, you can avoid potential errors and maintain a stable Laravel application even after significant changes.