0

Hello i'm having an error during registration of a new user. I have two database connections: first(default) oracle db for get data, second mysql db for users login, registration.

So i setup in database.php the default to oracle (so i don't have to use connection() method everytime).

In model User.php i put this line of code, so the login is pointing to mysql and i can login

protected $connection = 'mysql';

Everything is working fine until i try to register another user - i get an error because the registration is pointing to default oracle db. I could avoid this problem by switching the default again to mysql, but it's not the way i need.

What else i'm missing? How to point the registration to mysql db?

In database.php 'default' => env('DB_CONNECTION_ORA', 'oracle'),

I tried to add connection('mysql') on Schema user creation but it's still pointing the default.

RegisterController.php

class RegisterController extends Controller

{

 Register Controller



use RegistersUsers;


protected $redirectTo = RouteServiceProvider::HOME;
protected $connection = 'mysql';


public function __construct()
{
    $this->middleware('guest');
}

/**
 * Get a validator for an incoming registration request.
 *
 * @param  array  $data
 * @return \Illuminate\Contracts\Validation\Validator
 */
protected function validator(array $data)
{
    return Validator::make($data, [
        'name' => ['required', 'string', 'max:255'],
        'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
        'password' => ['required', 'string', 'min:8', 'confirmed'],
    ]);
}

/**
 * Create a new user instance after a valid registration.
 *
 * @param  array  $data
 * @return \App\Models\User
 */
protected function create(array $data)
{
    return User::connection('mysql')->create([
        'name' => $data['name'],
        'email' => $data['email'],
        'password' => Hash::make($data['password']),
    ]);
}

}

  • You can try this way: https://stackoverflow.com/questions/31847054/how-to-use-multiple-databases-in-laravel – Md Abu Musa Jul 06 '22 at 09:16
  • 1
    Does this answer your question? [How to use multiple databases in Laravel](https://stackoverflow.com/questions/31847054/how-to-use-multiple-databases-in-laravel) – aceraven777 Jul 06 '22 at 10:01
  • I've already read that thread, but there isn't a clear example of what i need. Anyway everything is setup like there. I get the problem only on register a new user –  Jul 06 '22 at 10:24

1 Answers1

0

I think you are using CREATE method. Try this, maybe works for you:

$user = new User;
$user->setConnection('your-another-db');
$user->name = $request->name;
...
...
...
$user->save();
MojSkin
  • 81
  • 1
  • 5
  • No it didn't help, i have a default register controller –  Jul 06 '22 at 10:29
  • Why don't you override your Controller or Method and also Route which is lead to this method? What I said here is a standard way to do what you want. Please override Controller ro Method and use a new Route. That should work fine. – MojSkin Jul 07 '22 at 03:41