5

I am using two database connections in my Laravel project.

I need to modify tables in both databases.

I have set up everything and now I am playing around and trying to get data from the first and the second database, but the docs hardly give information on how to do so, I only found this:

$users = DB::connection('foo')->select(...); 

Are there more resources somewhere? I was not able to find anything.

So far I understand that I cant use eloquent anymore if I use multiple databases? At least I cant use eloquent for the second database, which is not default.

For testing, I have created the same tables on both databases with different data. But I cant query the data, I always get this error:

Undefined property: Illuminate\Database\MySqlConnection::$User 

Here are my tests:

 $users1 = DB::connection('mysql_live')->select('SELECT * FROM users'); info($users1); $users2 = DB::connection('mysql')->User::all(); info($users2); 

I also tried:

$users1 = DB::connection('mysql_live')->User::all(); 

Best would be documentation on how to correctly use DB::connection and actually select, edit, insert data, etc.

EDIT

  • How do I actually f.e. select all users from both databases?
  • For the second database, I will never need to create new columns or tables, I will only need to update data, probably eloquent is not needed there, which means I always need to execute raw SQL, correct? If I use eloquent I need to create the models as well?
0

3 Answers 3

11
$users1 = DB::connection('mysql_live')->table('users')->get()->toArray(); 

For the second query you can do the same. There is no reason to use eloquent model for that. But if you want to then:

Inside your User model you must define your connection

 protected $connection = 'mysql'; //which is the default as well 

And also you must define the fields that you want to be retrieved like:

protected $fillable = ['email','username']; 

Really important is to hide sensitive fields like password etc etc. There you can use another array:

protected $hidden = ['password']; 

Then you can use the eloquent like:

$users = User::get(); 
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks a lot, very helpful. I just figured out aswell that I dont need to change anything for the default DB.
Glad i helped :)
My problem is, how do I know this syntax: ->table('users')->get()->toArray();. The laravel docu is really great, but the part on multiple databases is poor, there is only this one line: $users = DB::connection('foo')->select(...);. But I can figure that out step by step.
So basically this one casts your output of the users table into an array so you can use it. You can store it into a variable and access it as an array. This is a collection you can read more about it here: laravel.com/docs/5.8/collections#method-toarray
3

This is possible with the setConnection method

$someModel = new SomeModel; $someModel->setConnection('foo'); $something = $someModel->find(1); return $something; 

Comments

0

Try this code,

first you need to set the config

Config::set('database.connections.mysql_live.database', 'dbname'); Config::set('database.connections.mysql_live.host','localhost'); 

then

 $result=DB::connection('mysql_live')->table('users')->get(); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.