douhuifen9942
2017-03-24 05:51
浏览 566
已采纳

Laravel Eloquent - 包含snake_case和camelCase列混合的现有数据库

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,

图片转代码服务由CSDN问答提供 功能建议

我想要使用Laravel / Eloquent访问大型现有数据库。

  • 我正在使用Laravel 5.2,
  • 我使用“user11001 / eloquent-model-generator”软件包生成模型
  • 我使用命令 php artisan ide-helper:models

    生成自动完成但是我的数据库包含一个 混合使用snake_case和camelCase列,每当我尝试访问camelCase列“seoAlias”时,代码如下:

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

    我收到以下错误:

    SQLSTATE [ 42S22]:未找到列:1054未知列'seo_alias'在 'where子句'

    是否有人有一个解决方案来访问具有使用Laravel混合的snake_case和camelCase列?

    谢谢,

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • dougu5950 2017-03-25 14:05
    已采纳

    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.

    评论
    解决 无用
    打赏 举报
查看更多回答(2条)

相关推荐 更多相似问题