Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

bovisp's avatar
Level 10

Eloquent hasOne relationship across multiple databases

I am at wits end with this one. I have an Eloquent relationship that is:

Manager.php

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Manager extends Model
{
    protected $connection = 'moodlemanager';
    protected $table = 'managers';
    protected $fillable = ['moodle_id'];

    public function moodleUser()
    {
        return $this->hasOne('App\MoodleUser', 'id', 'moodle_id');
    }
}

MoodleUser.php

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class MoodleUser extends Model
{
    protected $conection = 'moodle';
    protected $table = 'mdl_user';
    protected $fillable = ['username', 'firstname', 'lastname'];

    public function Manager()
    {
        return $this->belongsTo('App\Manager', 'id', 'moodle_id');
    }
}

Here is my controller. I'm just doing a simple dd to test:

DashboardController

<?php

namespace App\Http\Controllers\Dashboard;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Http\Requests;
use App\Manager;
use App\MoodleUser;

class DashboardController extends Controller
{
    public function index(Manager $manager, MoodleUser $moodleUser)
    {
        $moodleUsersAreManagers = $moodleUser->has('manager')->get();
        dd($moodleUsersAreManagers);
    }
}

My migration for the manager is below. Moodle's database is created when you setup that LMS:

managers

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateManagersTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::connection('moodlemanager')->create('managers', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('moodle_id')->unique()->unsigned()->index();
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::table('managers', function (Blueprint $table) {
            //
        });
    }
}

I know this relationship works as I can do queries like the following in my controller:

$manager->find(141)->moodleUser()->get();

or eager load:

$manager->with('moodleUser')->get();

without any difficulties.

The problem arises when I try this:

$moodleUser->has('manager')->get();

I get this

QueryException in Connection.php line 761:

SQLSTATE[42S02]: Base table or view not found: 1146 Table 'moodle.managers' doesn't exist (SQL: select * from `mdl_user` where exists (select * from `managers` where `mdl_user`.`id` = `managers`.`moodle_id`))

It seams to me that Laravel is getting the database connections messed up as the managers table is in the moodlemanagers database and not the moodle database.

What I want is for Eloquent to return a Collection that contains only those Moodle users that have a manager relation in the managers table in the moodlemanagers connection.

Any help would be most appreciated. Thanks!

0 likes
3 replies
aand18's avatar

I have the same problem with multiple connections, Laravel doesn't include the database in the query... If anybody has a solution...

Seche's avatar

All you need to do is add the connection to the relationship in the models.

class Manager extends Model
{
    protected $connection = 'moodlemanager';
    protected $table = 'managers';
    protected $fillable = ['moodle_id'];

    public function moodleUser()
    {
        return $this->setConnection('moodle')->hasOne('App\MoodleUser', 'id', 'moodle_id');
    }
}

And the inverse for MoodleUser Model

class MoodleUser extends Model
{
    protected $conection = 'moodle';
    protected $table = 'mdl_user';
    protected $fillable = ['username', 'firstname', 'lastname'];

    public function Manager()
    {
        return $this->setConnection('moodlemanager')->belongsTo('App\Manager', 'id', 'moodle_id');
    }
}
manishjesani's avatar

I have the same problem with multiple connections, but this solution is not working on search.

Please or to participate in this conversation.