Skip to content

Updates table column fetching

Sam Sehnert requested to merge patch-fully-qualified-database-fields into master
  • Prevents errors if people are using fully qualified table names in cross-database situations

To test, use a multi-database setup. These databases should be on the same DB server. Create a second connection for your database, and use a trait which overwrites the models getTable() method. Something like:

<?php

namespace App\Models\Traits;

trait DefaultConnection
{
    public function getConnectionName()
    {
        return config('database.default');
    }

    public function getTable()
    {
        $dbName = $this->getConnection()->getDatabaseName();
        $table = parent::getTable();
        return $dbName === ':memory:' ? $table : $dbName . "." . $table;
    }
}

Once all that is done, create a model using this trait, and an API controller for that model. I found this issue when using a handleIndexActionRaw call, but it's likely to happen with all calls.

Ensure that after this patch, requests work as expected.

NOTE This patch also works if someone sets the database name directly into the table name on the model, E.g.,

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Thing extends Model
{
    protected $table = 'default.things';
}

Merge request reports