nicolaslopezj / searchable

A php trait to search laravel models
MIT License
2.01k stars 291 forks source link

Add db prefix to column names #115

Closed mrberggg closed 8 years ago

mrberggg commented 8 years ago

Added code to prefix column names with the DB's prefix as defined in config/database.php.

ikanc commented 8 years ago

Please note that you should use Config::get instead of config() as this is not support in 4.2. This crashed our production application.

mrberggg commented 8 years ago

@ikanc Ah, good point. Do you want to put in a pull request?

ikanc commented 8 years ago

@mrberggg Already did

jarnovanleeuwen commented 8 years ago

The connections array is indexed by connection name and not by driver name. Shouldn't this be something like:

protected function getColumns()
{
    if (array_key_exists('columns', $this->searchable)) {
        // Get connection name instead of driver.
        $key = $this->connection ?: Config::get('database.default');
        $prefix = Config::get("database.connections.{$key}.prefix");
        $columns = [];
        foreach($this->searchable['columns'] as $column => $priority){
            $columns[$prefix . $column] = $priority;
        }
        return $columns;
    } else {
        // Notice support for multi connections.
        return DB::connection($this->connection)->getSchemaBuilder()->getColumnListing($this->table);
    }
}

I also noticed that support for multiple connections was missing in this method.