Laravel – Override Eloquent Model’s default table name

For one of my client’s project I needed to use wordpress’s users table wp_users in Laravel’s User model. Because client wanted to use one database for WordPress and Laravel both. So I was forced to override eloquent model’s default table and use mine.

So suppose you have to use existing database, and you have my_cities table. But you can’t afford to rename your table. So easiest way is to tell your Laravel about this change. If your model name is City. Add protected property `$table` to your table like below.

Class City extends Model {
    protected $table = 'my_cities';
} 

If you want to override any Laravel package’s class or any other package that you have installed with composer command click here.

Hope this helps,
Enjoy Hacking !!!

Shyam has written 29 articles

Shyam is senior full stack developer, who loves to explore new technologies and work on them. He's passionate about coding so can code 24/7. He uses PHP as a backend programming language.

He knows Laravel, MySQL, AngularJS, ReactJS, Redis, Kubernetes, Git, CodeIgniter, PHP, MVC pattern, Lodash, jQuery, VanilaJS, Teamcity and many other technologies and tools.

Shyam writes notes and hacks on his blog (https://shyammakwana.me). In spare time he can be found @ StackOverflow or crafting any new open source application.

Passionate Programmer and Meditator #PERIOD.