Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

MMIT's avatar
Level 5

Help with renaming a Model in Laravel and Filament!

Hello All -

Thanks in advance, I've been struggling with this for a few weeks, so any help is greatly appreciated. We are developing a multi tenant SAAS application with Laravel 11 and Filament. We are using the multi-tenant functionality and originally made the model for the tenant as "Organization" - but are not trying to refactor to "Team" to better match the language of Laravel/Filament. My plain is pretty simple:

  1. Rename tables via a migration (https://laravel.com/docs/11.x/migrations#renaming-and-dropping-tables)
  2. Rename the model, Factory, and test files along with refactor references to Organization.

Unfortunately we tend to do testing with artisan:migrate fresh and when doing so I get an error in my originally migration where I created the Organization model and create a foreignKey on the User model. If I never had to rerun the original migration it would probably work fine, but how can we refactor so we can keep using migrate fresh?

I found this tutorial with an alias, but I can't get the migrations to work either way:

https://joeczubiak.com/how-to-rename-a-widely-used-model-in-laravel/

Anyone had this issue before, can anyone provdie a recommendation or best practice for this situation?

Thanks!

0 likes
3 replies
LaryAI's avatar
Level 58

Renaming a model and its associated database table in a Laravel application, especially in a multi-tenant setup, requires careful planning to ensure that all references are updated correctly. Here's a step-by-step guide to help you refactor your "Organization" model to "Team" while maintaining the ability to use artisan migrate:fresh.

Step 1: Rename the Database Table

First, create a new migration to rename the table. You can use the rename method in a migration to achieve this:

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class RenameOrganizationToTeam extends Migration
{
    public function up()
    {
        Schema::rename('organizations', 'teams');
    }

    public function down()
    {
        Schema::rename('teams', 'organizations');
    }
}

Step 2: Update Foreign Keys

If you have foreign keys referencing the organizations table, you'll need to update them. This might involve dropping the existing foreign keys and creating new ones. You can do this in the same migration or a separate one:

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class UpdateForeignKeysForTeam extends Migration
{
    public function up()
    {
        Schema::table('users', function (Blueprint $table) {
            $table->dropForeign(['organization_id']);
            $table->foreign('team_id')->references('id')->on('teams');
        });
    }

    public function down()
    {
        Schema::table('users', function (Blueprint $table) {
            $table->dropForeign(['team_id']);
            $table->foreign('organization_id')->references('id')->on('organizations');
        });
    }
}

Step 3: Rename the Model and Update References

  1. Rename the Model File: Rename Organization.php to Team.php.

  2. Update the Model Class: Change the class name inside the file:

    namespace App\Models;
    
    use Illuminate\Database\Eloquent\Model;
    
    class Team extends Model
    {
        // Model code here
    }
    
  3. Update Factory and Test Files: Rename any factory and test files related to the Organization model to Team and update their contents accordingly.

  4. Refactor Codebase: Use your IDE's search and replace functionality to update all references from Organization to Team. This includes:

    • Controller methods
    • Service classes
    • Blade views
    • Any other part of your application where the model is referenced

Step 4: Handle Migrate Fresh

To ensure migrate:fresh works without issues, make sure your migrations are ordered correctly. The migration that creates the organizations table should be updated to create the teams table instead. This way, when you run migrate:fresh, it will create the correct table from the start.

Step 5: Test Thoroughly

After making these changes, thoroughly test your application to ensure everything works as expected. Pay special attention to areas where the Organization model was heavily used.

By following these steps, you should be able to refactor your model and database table while maintaining the ability to use artisan migrate:fresh.

MMIT's avatar
Level 5

@LaryAI

Thanks! One Issue I see, when using your suggestion, in a production env. I a have already run the migration to create the organization, and future deploys will not run that migration again, so changing that file won't actually do anything in my production env. Correct?

jaseofspades88's avatar

Lary won't reply to you, @MMIT. If your application is already in production, create new migrations that drop or rename your tables and update the class names accordingly. You might need to drop constraints temporarily whilst making changes but the problem you have with making these changes in production, if you have data in these tables you wish to change, back up first because you'll need to migrate it all to the new schema.

It might be easier for you to export the data as it is, update your schema and then update the export script to add data to the new files.

If your site is already in production and you're looking to make such crucial changes to the database, I'd suggest you might have rushed to production.

Please or to participate in this conversation.