hoyvoy / laravel-cross-database-subqueries

Eloquent cross database compatibility in subqueries
MIT License
103 stars 45 forks source link

Undefined table homestead.users #6

Closed daemswibowo closed 6 years ago

daemswibowo commented 6 years ago

Description:

i want to check if customers has users which is they are in different database but i get error homestead.users does not exists when i query from Customer model

Steps To Reproduce:

I have 2 database e.g:

DB_CONNECTION=pgsql
DB_HOST=127.0.0.1
DB_PORT=5432
DB_DATABASE=homestead
DB_USERNAME=homestead
DB_PASSWORD=secret

DB_CONNECTION2=pgsql
DB_HOST2=103.xxx.xxx.xxx
DB_PORT2=5432
DB_DATABASE2=db_mapping
DB_USERNAME2=xxx
DB_PASSWORD2=xxx

the users table is in local (DB_CONNECTION) and the customers table is in remote server (DB_CONNEcTION2)

the query is:

return Customer::has('pj')->limit(10)->get();

in customer model :

<?php

namespace App;

use Hoyvoy\CrossDatabase\Eloquent\Model;

class Pelanggan extends Model
{
    protected $connection = 'pgsql2';
    protected $table = 'customers';

    public function pj ()
    {
        return $this->belongsTo('App\UserDua', 'id_peg', 'id_peg');
    }
}

and i get this error message:

SQLSTATE[42P01]: Undefined table: 7 ERROR: relation \"homestead.users\" does not exist\nLINE 1: ...ct * from \"customers\" where exists (select * from \"homestead...\n ^ (SQL: select * from \"customers\" where exists (select * from \"homestead\".\"users\" where \"customers\".\"id_peg\" = \"users\".\"id_peg\") limit 10)
maguilar92 commented 6 years ago

@daemswibowo Thats because your databases are in different hosts. This package only works with databases in same server. You must do it by code because this package can't do it.

daemswibowo commented 6 years ago

Thank you for your answer @maguilar92, that's true. I finally ended by not joining the table.