Laravel 9 Muliple数据库连接
您好,我在新用户注册期间遇到了错误。 我有两个数据库连接:第一个用于获取数据的Oracle DB,第二个用于用户登录的MySQL DB,注册。
因此,我在database.php中设置默认值为Oracle(因此我不必每次使用Connection()方法)。
在Model user.php中,我放置了此代码行,因此登录指向MySQL,并且可以登录
protected $connection = 'mysql';
所有内容都可以正常工作,直到我尝试注册另一个用户 - 我会遇到错误,因为注册指向默认的Oracle DB。 我可以通过再次将默认值切换到MySQL来避免此问题,但这不是我需要的方式。
我还想念什么?如何将注册指向MySQL DB?
在database.php 'default'=> env('db_connection_ora','oracle'),
我尝试在架构用户创建上添加连接('mysql'),但仍指向默认值。
regize controller.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']),
]);
}
}
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']),
]);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您正在使用创建方法。尝试一下,也许对您有用:
I think you are using CREATE method. Try this, maybe works for you: