nanadjei2
4831
14
Eloquent

Laravel 5 multiple database connection.

Posted 3 years ago by nanadjei2

I am developing a system that fetches data from two databases. I want to do a login function but direct the function to the other database(mysql2 in config/database) to the other database and not default database(mysql). This is my config/database.php

  # Knust primary database connection
 'mysql' => [
        'driver'    => 'mysql',
        'host'      => env('DB_HOST', 'localhost'),
        'database'  => 'hofsa_knust',
        'username'  => 'root',
        'password'  => '',
        'charset'   => 'utf8',
        'collation' => 'utf8_unicode_ci',
        'prefix'    => '',
        'strict'    => false,
    ],

         # Legon primary database connection
         'mysql2' => [
        'driver'    => 'mysql',
        'host'      => env('DB_HOST', 'localhost'),
        'database'  => 'hofsa_legon',
        'username'  => 'root',
        'password'  => '',
        'charset'   => 'utf8',
        'collation' => 'utf8_unicode_ci',
        'prefix'    => '',
        'strict'    => false,
    ],

This is my controller

 <?php

 namespace App\Http\Controllers;
 use Illuminate\Support\Facades\Validator;
 use Illuminate\Support\Facades\Hash;
 use Illuminate\Support\Facades\Auth;
 use App\Http\Requests;
 use App\Http\Controllers\Controller;
 use App\Http\Requests\CreateUserRequest;
 use UxWeb\SweetAlert;
 use App\LegonUser;
 use Request;
 use Alert;
 use DB;
 class LegonUserController extends Controller
    {
  public function LegonSignin() {
   $input = Request::all();
  $user_data = [
  'username' => $input['username'],
  'password' => $input['password']
];

//$database = LegonUser::on('mysql2')->select('*')->where($user_data ,'=',         $user_data)->get();
$legonModel = new LegonUser;
$legonDB = $legonModel->setConnection('mysql2');

if ($legonDB) {
  if (Auth::attempt($user_data)) {
    //Alert::success('You Have Successfully Signed In', 'Great !');
    alert()->success('You Have Successfully Signed In', 'Great !')->autoclose(2500);
    return redirect('/');
  } else {
    return redirect()->back()->withErrors($user_data);
  }
}

} }

This is my Eloquent Model

 <?php

namespace App;

use Illuminate\Auth\Authenticatable; use Illuminate\Database\Eloquent\Model; use Illuminate\Auth\Passwords\CanResetPassword; use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract; use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;

class LegonUser extends Model implements AuthenticatableContract, CanResetPasswordContract { use Authenticatable, CanResetPassword;

/**
 * The database name used by the model.
 *
 * @var string
 */
protected $connection = 'mysql2';
/**
 * The database table used by the model.
 *
 * @var string
 */
protected $table = 'users';

/**
 * The attributes that are mass assignable.
 *
 * @var array
 */
protected $fillable = ['firstname', 'lastname', 'username', 'nickname', 'password'];

/**
 * The attributes excluded from the model's JSON form.
 *
 * @var array
 */
protected $hidden = ['password', 'remember_token'];

} After all these, it still goes to the default database and not the custom one(mysql2).

Please sign in or create an account to participate in this conversation.

Reply to

Use Markdown with GitHub-flavored code blocks.