Home » Php » php – Laravel whereHas across two separate databases

php – Laravel whereHas across two separate databases

Posted by: admin July 12, 2020 Leave a comment

Questions:

Problem: I want to get Customers only if they have already placed an order for the current year. Customers and Orders are on two separate Databases (this can’t change). The relationships are all setup and working correctly but when I try the following I keep getting an SQL error as it is trying to search ‘orders’ on the ‘customers’ database. Is there anyway to force Laravel to use the correct database in this scenario?

$customers = $customers->whereHas('orders', function($query){
    $query->where('academic_year_id', '=', $this->current_academic_year->id);
});

$customers = $customers->orderBy('DFKEY','ASC')->get();

Order Model:

public function customer()
{
    return $this->belongsTo('Customer','dfkey');
}

Customer Model:

protected $connection = 'mysql2';

public function orders()
{
    return $this->hasMany('Order','dfkey','DFKEY');
}

Thanks in advance!

How to&Answers:

Solved this by using a filter:

public function index()
{   
    $customers = new Customer;
    // Make sure customers are current parents of students
    $customers = $customers->whereHas('students', function($q) {
        $q->where('STATUS', '=', 'FULL');
    });

    //Filter results
    if(Input::get('academic_year') == 'ordered'){
        $customers = $customers->orderBy('DFKEY','ASC')->get();
        $filtered = $customers->filter(function($customer)
        {
            if ($customer->orders()->where('academic_year_id','=',$this->current_academic_year->id)->first()) {
                return true;
            }
        });
        $customers = $filtered;
        return View::make('admin.customers.index',compact('customers'));
    }

    $customers = $customers->orderBy('DFKEY','ASC')->get();

    return View::make('admin.customers.index',compact('customers'));
}

Answer:

Try to write query like this and put your database and tablename as it is->

$customers = Schema::connection('your_database_name')::table('respective_table_name')
->where('academic_year_id', '=', $this->current_academic_year->id)
->orderBy('DFKEY','ASC')
->get();

Answer:

I ‘ve solved this by adding ‘connection’ and ‘table’ variables models specificing the database in wich the model is saved.

For example, i have a model called ‘User’ in the database called ‘core_database’ in the table users.
In the other side, i have a model called ‘UserEvents’ in the database called ‘logs_database’ in the table ‘user_events’

So i will have a two conections on config/database.php file:

'core_db_connection' => [
        'driver' => 'mysql',
        'host' => host_ip,
        'port' => port,
        'database' => 'core_database',
        'username' => username,
        'password' => password,
        ....
    ],

'logs_db_connection' => [
        'driver' => 'mysql',
        'host' => host_ip,
        'port' => port,
        'database' => 'logs_database',
        'username' => username,
        'password' => password,
        ....
    ],

And the models will be like:

class User extends Authenticatable {
  protected $table = 'core_database.users';
  protected $connection = 'core_db_connection';
  ...
}

class UserEvents extends Model {
  protected $table = 'logs_database.user_events';
  protected $connection = 'logs_db_connection';
  ...
}

This was tested in databases in the same database server.
Database connections have the same host ip.
I have not tried with a different way

Using this configuration, i can make any query across two or more separeted database in the same database server, in my case, RDS.

I hope this help you!