Site icon Shyam Makwana

Laravel – Override Eloquent Model’s default table name

override-laravel-eloquent-default-table

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 !!!

Exit mobile version