'Is it possible to change the table name in the migration file-laravel

I created a migration file using

php artisan make:migration create_todoapps_table.

The migration file was created with the table name as to_do_apps

(Schema::create('to_do_apps', function (Blueprint $table) {...}).

But I don't want the table name to be to_do_apps .

So I tried to change it to todoapps manually

(Schema::create('todoapps', function (Blueprint $table) {...}).

When doing migration this works fine and table with todoapps is getting created. But when doing the factory and seed, it is trying to add data to the old table name to_do_apps itself, which is causing an error because, that table is not available.

Is there a way to overcome this issue?

How can I change the tablename in migration file without breaking anything?



Solution 1:[1]

The name of the table to be used for a model comes from the model itself, not from migrations.

Laravel assumes that the name of the table for a model is the snake_case of the model name. If you want to use the table todoapps, just add the following line to the model that should read data from that table (probably your model is called ToDoApps)

protected $table = 'todoapps';

Solution 2:[2]

You can set the table name in the Eloquent model if its not in the default naming convention. Below is from the Laravel Docs

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Flight extends Model
{
    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'my_flights';
}

The seed which is using the model will then use the correct table name.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 gbalduzzi
Solution 2