August 31st, 2021

Change eloquent table name

Change eloquent table name
Sponsored by

I am caught up working on a ERP application for the Romanian furniture manufacturing industry so I named my tables in my native language. The problem with that were the suffixes that Laravel is adding (user -> users) and I ended up having a romanian name for the table ending in “s”.

1public function up()
2{
3 Schema::rename('old_table_name', 'new_table_name');
4}

Once you run the migration the table names will be changed BUT you may encounter some errors saying that the “old_table_name” could not be found. That’s because Laravel is trying to guess the table name and if this happens you should explicitly specify the table name in your Eloquent model like this:

1class NameOfTheClass extends Model
2{
3 public $table = 'new_table_name';
4}

If you enjoyed this article, please consider supporting our work for as low as $5 / month.

Sponsor
Marian Pop

Written by

Marian Pop

Writing and maintaining @LaravelMagazine. Host of "The Laravel Magazine Podcast". Pronouns: vi/vim.

Comments

Stay Updated

Subscribe to our newsletter

Get latest news, tutorials, community articles and podcast episodes delivered to your inbox.

Weekly articles
We send a new issue of the newsletter every week on Friday.
No spam
We'll never share your email address and you can opt out at any time.