{"action":"created","issue":{"url":"https://api.github.com/repos/JosephSilber/bouncer/issues/28","labels_url":"https://api.github.com/repos/JosephSilber/bouncer/issues/28/labels{/name}","comments_url":"https://api.github.com/repos/JosephSilber/bouncer/issues/28/comments","events_url":"https://api.github.com/repos/JosephSilber/bouncer/issues/28/events","html_url":"https://github.com/JosephSilber/bouncer/issues/28","id":125485476,"number":28,"title":"Problem with multiple databases","user":{"login":"renanBritz","id":5933645,"avatar_url":"https://avatars.githubusercontent.com/u/5933645?v=3","gravatar_id":"","url":"https://api.github.com/users/renanBritz","html_url":"https://github.com/renanBritz","followers_url":"https://api.github.com/users/renanBritz/followers","following_url":"https://api.github.com/users/renanBritz/following{/other_user}","gists_url":"https://api.github.com/users/renanBritz/gists{/gist_id}","starred_url":"https://api.github.com/users/renanBritz/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/renanBritz/subscriptions","organizations_url":"https://api.github.com/users/renanBritz/orgs","repos_url":"https://api.github.com/users/renanBritz/repos","events_url":"https://api.github.com/users/renanBritz/events{/privacy}","received_events_url":"https://api.github.com/users/renanBritz/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":19,"created_at":"2016-01-07T21:12:06Z","updated_at":"2016-01-08T21:56:39Z","closed_at":null,"body":"Hello!\r\n\r\nThank you for this awesome project, it makes handling roles and abilities a painless task.\r\n\r\nApparently Bouncer does not support multiple databases...\r\n\r\nFor my project I have my users table on a different database, and trust me, I have good reasons to do that.\r\n\r\nThe problem is: when I try to assign a role to a user, I get a QueryException, saying that the users table was not found.\r\n\r\nI modified the migrations to set the foreign keys correctly, but I think it would be a bad practice to modify the vendor files and fix the relations in the classes...\r\n\r\nIs there any way you could help me here?\r\n\r\nThank you!"},"comment":{"url":"https://api.github.com/repos/JosephSilber/bouncer/issues/comments/170139132","html_url":"https://github.com/JosephSilber/bouncer/issues/28#issuecomment-170139132","issue_url":"https://api.github.com/repos/JosephSilber/bouncer/issues/28","id":170139132,"user":{"login":"Arcesilas","id":5526263,"avatar_url":"https://avatars.githubusercontent.com/u/5526263?v=3","gravatar_id":"","url":"https://api.github.com/users/Arcesilas","html_url":"https://github.com/Arcesilas","followers_url":"https://api.github.com/users/Arcesilas/followers","following_url":"https://api.github.com/users/Arcesilas/following{/other_user}","gists_url":"https://api.github.com/users/Arcesilas/gists{/gist_id}","starred_url":"https://api.github.com/users/Arcesilas/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Arcesilas/subscriptions","organizations_url":"https://api.github.com/users/Arcesilas/orgs","repos_url":"https://api.github.com/users/Arcesilas/repos","events_url":"https://api.github.com/users/Arcesilas/events{/privacy}","received_events_url":"https://api.github.com/users/Arcesilas/received_events","type":"User","site_admin":false},"created_at":"2016-01-08T21:56:38Z","updated_at":"2016-01-08T21:56:38Z","body":"Ok, actually, that's pretty easy.\r\n\r\nIn user Model, remove the connection and set the table name:\r\n\r\n```php\r\n<?php\r\n\r\nnamespace App;\r\n\r\n//use Silber\\Bouncer\\Database\\HasRolesAndAbilities;\r\nuse Illuminate\\Foundation\\Auth\\User as Authenticatable;\r\n\r\nclass User extends Authenticatable\r\n{\r\n\r\n    use HasRolesAndAbilities;\r\n\r\n    //protected $connection = 'mysql2';\r\n\r\n    protected $table = 'test2.users';\r\n```\r\n\r\nForget everything about Clipboard. You can remove the extended one and leave the `HasRolesAndAbilities` trait untouched as after my first answer:\r\n\r\n```php\r\n<?php\r\n\r\nnamespace App;\r\n\r\ntrait HasRolesAndAbilities\r\n{\r\n    use \\Silber\\Bouncer\\Database\\HasRolesAndAbilities;\r\n\r\n    public function roles()\r\n    {\r\n        return $this->belongsToMany(Models::classname(Role::class), 'test.user_roles');\r\n    }\r\n\r\n    public function abilities()\r\n    {\r\n        return $this->belongsToMany(Models::classname(Ability::class), 'test.user_abilities');\r\n    }\r\n\r\n}\r\n```\r\n\r\nAnd that's all. What happens is that we do not use 2 connections, which is not really necessary, since we are on the same server. If your databases were on 2 different servers, it would be different and relations would not work at all.\r\n\r\nSo, we only use 1 connection, set the permissions to allow the user access both databases and specify that users table is in the second database. Eloquent is able to correctly escape the table name when it has the database name in it:\r\n```\r\nprotected $table_name = 'database.table';\r\n```\r\nwill result in\r\n```\r\n`database`.`table`\r\n```\r\nIt's very simple, actually..."}}