0

I'm working on a project with Laravel 4 where I use a postgres-db. The public-schema is the main schema for the admin. All the other custom schemas are for clients. All schemas are having the same tables setup.

But for some clients, the must retrieve data multiple schemas. Is this possible with L4? Or do I have to write custom queries?

These are my settings for the db-connection

'pgsql' => array(
        'driver'   => 'pgsql',
        'host'     => 'localhost',
        'database' => 'postgres',
        'username' => 'postgres',
        'password' => 'root',
        'charset'  => 'utf8',
        'prefix'   => '',
        'schema'   => 'public, client1, client2',
    ),

But when I'm preforming the query:

$users = Users::all()
**OR**
$users = DB::select(DB::raw('SELECT * FROM client1.users, client2.users'));

I'm only retrieving the users from public. Did I miss something or isn't this possible with Laravel?

Thanks in advance

3 Answers 3

2

Its fairly easy to do with Laravel.

In your config:

...

'pgsql2' => array(
    'driver'   => 'pgsql',
    'host'     => 'localhost',
    'database' => 'postgres',
    'username' => 'postgres',
    'password' => 'root',
    'charset'  => 'utf8',
    'prefix'   => '',
    'schema'   => 'client1',
),

...

In your model:

// Define your second db connection
protected $connection = 'pgsql2';
// Define your table
protected $table = 'users';

If have not tested this, so if that doesn't work:

// Define your table
protected $table = 'client1.users';

in your model.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your answer, but I was hoping if I could get records from multiple schema's in one query. But after some research, I figured it out that it isn't possible. Or I am wrong?
You should be able to define relationships on your models the same way as a single schema(I have done this and it works) and use Eloquent to query your data. The fluent query builder should work the same way, just make sure your table is defined as I have shown above.
1

You don't need to specify other schema in your config, leave it as public.

Your query was wrong - that's all. This is how it should be:

SELECT * FROM client1.users UNION ALL SELECT * FROM client2.users

This query will select everything from both tables. You can UNION ALL selects from tables as many you need.

You may want to determine which user is in which schema, so just add an identifier (variable) in your query:

SELECT 1 AS s, * FROM client1.users
UNION ALL
SELECT 2 AS s, * FROM client2.users
UNION ALL
SELECT 3 AS s, * FROM client3.users

The output would look like this:

| s | id | name   |
|---|----|--------|
| 1 | 1  | John   |
| 1 | 2  | Lucy   |
| 2 | 1  | Robert |
| 3 | 1  | Kelly  |
| 3 | 2  | Sam    |

Comments

0

You need to use

'schemas' => 'public, client1, client2'

if you want to use multiple schemas.

For example when you are using procedures from different schemas.

If you will use

'schema' => 'public, client1, client2'

it will choose the first in the list.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.