0

In Laravel 4, how do you use a table from another schema (other than the default schema defined in database.php) to query against?

The L4 docs do not show any sign of being able to do this...

1
  • hi, did you find the answer to this? Commented Dec 4, 2014 at 23:08

2 Answers 2

3

http://laravel.com/docs/database

define another schema config in the connections array

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

then just switch to it, per the docs:

$users = DB::connection('foo')->select(...);
Sign up to request clarification or add additional context in comments.

2 Comments

Can I ask how to use this: $users = DB::connection('foo')->select(...); Does foo refers to either pgsql', mysql`, etc?
how does this answer the question of accessing different schemas in the same database? you just said how to configure more than one connection and both refer to the same schema (public). the OP is asking how you would do a query like this: select * from security.user inner join company.person on (...) he wants to know how do you access both security and company schemas at the same time.
1

the question was: How to use an alternate schema in queries?

the answer was to define another schema, then choose to connect to it. example:

$schema_conn = DB::connection('{ARRAY_KEY_FOR_OTHER_SCHEMA_CONFIG}');
$query = $schema_conn->where('property', 'value')->get();

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.