Reputation: 853
I have developed a project using Laravel 5.2 and I used 2 MySQL connections and configured as given below in config.database.php
.
'mysql1' => [
'driver' => 'mysql',
'host' => 'localhost',
'port' => '3306',
'database' => 'main_data',
'username' => 'root',
'password' => 'root',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
'engine' => null,
],
'mysql2' => [
'driver' => 'mysql',
'host' => 'localhost',
'port' => '3306',
'database' => 'second',
'username' => 'root',
'password' => 'root',
'charset' => 'utf8_table',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
'engine' => null,
],
And the model that I want to use second database connection is given below.
use Illuminate\Database\Eloquent\Model as Eloquent;
class package extends Eloquent {
//use HybridRelations;
protected $connection = 'mysql2';
protected $table = 'PACKAGE';
...
But when I tried to create a new package in controller using package::create(data)
. it gives error Base table or view not found: 1146 Table 'main_data.package' doesn't exist' in ...
It seems the model connected to the default connection and when I search the only solution I could found is add protected $connection = 'mysql2';
which I already did.
Please help me to solve this issue.
Upvotes: 0
Views: 66
Reputation: 17216
The $connection
attribute in on the Illuminate\Database\Eloquent\Model::class
change your Package::class
to extend it
use Illuminate\Database\Eloquent\Model;
class package extends Model {
//use HybridRelations;
protected $connection = 'mysql2';
protected $table = 'PACKAGE';
Upvotes: 1