How to change the Auth database of Laravel 4.2?

Asked

Viewed 769 times

0

My project has two databases: one for the user account and another for the project domain. My need is to define the database that the Auth uses for authentication, because in the default configuration the domain database is defined.

return array(

'default' => 'mysql_domain', 

I wanted to change this definition above to the Auth and so it does not use the domain database.

'connections' => array(

    'mysql_account' => array(
        'driver'    => 'mysql',
        'host'      => 'localhost',
        'database'  => 'user_agro_admin',
        'username'  => 'root',
        'password'  => '',
        'charset'   => 'utf8',
        'collation' => 'utf8_unicode_ci',
        'prefix'    => '',
    ),

    'mysql_domain' => array(
        'driver'    => 'mysql',
        'host'      => 'localhost',
        'database'  => 'indicator_agro_admin',
        'username'  => 'root',
        'password'  => '',
        'charset'   => 'utf8',
        'collation' => 'utf8_unicode_ci',
        'prefix'    => '',
    )

),

In the configuration of auth it is possible to just define the driver, model and table. My need could be solved only if there was the method Auth::setDefaultConnection('mysql_account') or something similar. How to do this?

P.S.: I don’t want to change the default setting with the method Config::set('database.fo', 'bar'), and yes, change the database that the Auth uses!

P.S.2.: I use Query Builder.

1 answer

1

Wouldn’t it be possible to just change the connection in the User model? According to the documentation of the Eloquent, it is possible to do this:

protected $connection = 'connection-name';
  • Sorry, I forgot to inform you that I am not using Eloquent. I use Query Builder.

  • Any particular reason you’re not using Eloquent for authentication? Something else: how are you doing the authentication? With Auth::Attempt or manually via query Builder?

  • I find the Eloquent "cast" for complex projects. Mainly, when it comes to making consultations with various relationships. Anyway, it’s a personal matter. I authenticate with Auth::Attempt and it is precisely the problem that I am facing because Auth does the query in the domain database, because it is set as the default in the settings.

  • I already think the opposite, I find the Eloquent very powerful if well used, but anyway, it’s not the case... In this case, you have two options: 1- Change the configuration in config/auth.php to use Eloquent (in this case, it will use only for authentication) and do what I commented above. 2- Search the database manually via DB::Connection('') and then authenticate it via Auth::login($objectUsuario); I don’t know if an instance of Model is required or not, but the documentation says it has to be an object.

  • But a query with Query Builder returns a stdClass and the login method requires a Userinterface object.

  • The easiest way is to use Eloquent, even if only for authentication, by configuring auth.php properly and creating/changing the User model. If you still don’t want to use it, Laravel has the Illuminate Auth Genericuser class that implements the Userinterface interface and can be used as a "generic model" in Auth::login(). This class is even used by Laravel when using the "database" driver for authentication. See: https://github.com/laravel/framework/blob/v4.2.18/src/Illuminate/Auth/DatabaseUserProvider.php#L100

  • As my deadline is short I opted for simplicity. I modified the default database.php file configuration to mysql_account so that Auth can only authenticate using the usual Attempt method. And in my models I use the Connection method, for example, DB::Connection('mysql_domain')->table('Servers')::get(). Anyway, not exactly what I wanted.

  • But why don’t you just use Eloquent for authentication? Keeps the database.php the way it was and sets only auth.php to use Eloquent. Creates a model for the user (which extends Eloquent) and uses it only to authenticate. This will not impact anything on your project and is even simpler than your solution...

  • Trauma after using it for complex consultations.

  • 1

    Dude, Eloquent will be used ONLY for authentication, with NO impact for the rest of the application. Your solution will impact any resource that uses the default connection, this includes the "php Artisan migrate" command, which in this case will migrate to the wrong base... If you still want to keep it that way, beautiful. But be aware that it can create more problems.

  • No, I put Migrations in specific directories and when I run the Artisan migrate command add the path wildcard.

  • @galegostigma your solution will bring bigger problems to your application. If you take a look at https://laravel.com/docs/4.2/eloquent you will see that eloquent is very simple and powerful. You can make very complex querys.

  • @Mateusazevedo agree with you +1 :)

Show 8 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.