1

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,

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

It's not as type-safe as I would like it, but it definitely works. So I guess it will have to do
1

It's a bit tricky but you can remove the Str::snake from the QueryBuilder. For that you need to extends the base model and override newBaseQueryBuilder() to use our own QueryBuilder.

<?php

use Illuminate\Database\Query\Builder as QueryBuilder;

abstract class Model extends \Illuminate\Database\Eloquent\Model
{
    protected function newBaseQueryBuilder()
    {
        $connection = $this->getConnection();

        return new class(
            $connection,
            $connection->getQueryGrammar(),
            $connection->getPostProcessor()
        ) extends QueryBuilder {
            protected function addDynamic($segment, $connector, $parameters, $index)
            {
                $bool = strtolower($connector);

                // here we remove the Str::snake (in illuminate/database/Query/Builder.php)
                $this->where($segment, '=', $parameters[$index], $bool);
            }
        };
    }
}

Now extends this model and it should works.

Comments

-1

I think it's fine how it is. Eloquent required camel_case in L3, and I don't think it's super practical to have people convert their entire databases to camelCase for L4. Remembering when to use what is pretty easy:

Class names are study case and methods are camel case (PSR-1).
Eloquent attributes can be accessed using either snake or camel case, but database tables and columns are snake case.
Configuration options, language lines, and view names are snake case and use "dot" syntax.
Global functions are snake case per PHP convention.
Input can be whatever you want.

That's really all there is to it.

1 Comment

I don't think it's practical to rename half the columns of a large existing database in order to make it work with Laravel

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.