Reputation: 1640
connection('mysql2') is my (working) second database connection.
When I migrate first, connection('mysql2') is working like expected, the table is created.
Schema::connection('mysql2')->create('brands', function(Blueprint $table)
{
//...
});
But when I try to seed tables in my second database:
use Illuminate\Database\Seeder;
use Illuminate\Database\Eloquent\Model;
use App\Brands;
class DatabaseSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
Model::unguard();
$this->call('BrandsTableSeeder');
$this->command->info("Brands table seeded.");
}
}
class BrandsTableSeeder extends Seeder
{
public function run()
{
DB::connection('mysql2')->table('brands')->delete();
Brands::connection('mysql2')->create(['brand' => 'test']);
}
}
I got:
[BadMethodCallException]
Call to undefined method Illuminate\Database\Query\Builder::connection()
Upvotes: 2
Views: 5507
Reputation: 3943
Problem with your code is you have used Connection()
method with Eloquent(not DB), Eloquent doesn't have connection()
method.
You can use on()
method with model(Eloquent) to specify connection
$user = User::on('mysql2')->create(['brand' => 'test']);
reference http://laravel.com/docs/4.2/eloquent#basic-usage
or
instead of writing on('mysql2')
everywhere
you can write following code in model
protected $connection = 'mysql2';
and now seed is written as
class BrandsTableSeeder extends Seeder
{
public function run()
{
Brands::truncate();
Brands::create(['brand' => 'test']);
}
}
Upvotes: 7