Search code examples
phpmysqllaraveleloquentcamelcasing

Laravel Eloquent - Existing Database with Mix of snake_case and camelCase Columns


I have a large existing database that I would like to access with Laravel/Eloquent.

  • I'm using Laravel 5.2,
  • I have generated models using the "user11001/eloquent-model-generator" package
  • I have generated autocompletion using the command php artisan ide-helper:models

However my database contains a mix of snake_case and camelCase columns, and whenever I try to access the camelCase column "seoAlias", with code like the following:

$x = AdvertiserModel::whereSeoAlias('someValue')->get();

I get the following error:

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'seo_alias' in 'where clause'

Does anyone have a solution for accessing a database with a mix of snake_case and camelCase columns using Laravel?

Thanks,


Solution

  • You could use the following if your column name is seoAlias:

    $x = AdvertiserModel::where('seoAlias','=','someValue')->get();
    

    This way you will be able to access the database with eloquent without having to rename the columns.