0

I have a multi-connection application. Model Department is on a different connection from model CutOff.

I have a relationship in my CutOff that looks like this:

public function department()
{
    $department = new Department();
    $database = $department->getConnection()->getDatabaseName();

    return $this->belongsTo('App\Department', 'department_id',    "$database.department.id");
}

So I try to call it like this from my view ...

$cut_off->department->name;

I get this error:

ErrorException (E_ERROR)

SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '.id = ? limit 1' at line 1 (SQL: select * from departments where departments.uni_main.department.id = 2 limit 1) (View: C:\wamp\www\university\resources\views\utme\dashboard\cut_off\index.blade.php)

Please how do I get around this?

Kenny Horna
  • 13,485
  • 4
  • 44
  • 71
  • Possible duplicate of [belongsToMany relationship in Laravel across multiple databases](https://stackoverflow.com/questions/25142968/belongstomany-relationship-in-laravel-across-multiple-databases) – Kenny Horna Jan 15 '19 at 14:32
  • @HCK I tried it out and i get this error ** ErrorException (E_ERROR) SQLSTATE[42S02]: Base table or view not found: 1146 Table 'uni_utme.departments' doesn't exist (SQL: select * from `departments` where `departments`.`uni_utme`.`departments` = 2 limit 1) ** – Inspired Prynce Kaka Jan 15 '19 at 14:40

1 Answers1

0

Set protected $connection = 'the_other_db'; on the Department model which matches what is configured in database.php

Then I can just use

public function department()
{
   return $this->belongsTo('App\Department', 'department_id');
}