Hello,
I'm trying to do this (Laravel 4.2) :
In database.php
'default' => '',
In a function
$model = new EloquentModel();
$model->setConnection('connection_name');
$result = $model->find(1);
I have this error : 'Database [] not configured'.
If I put a wrong connection_name (i.e : 'toto'), I got this error : 'Database [toto] not configured'
Same error with (in EloquentModel.php) :
protected $connection = 'connection_name';
It seems in a non-static context, 'setConnection' and 'connection' property don't work properly. Am I correct ?
p.s : The code above works fine, but it's not the point.
EloquentModel::on('connection_name')->find(1);
Hi catalisio,
It's because the find method create a new instance of query.
public static function find($id, $columns = array('*'))
{
if (is_array($id) && empty($id)) return new Collection;
$instance = new static;
return $instance->newQuery()->find($id, $columns);
}
That why don't use find and use a query like:
$model = new EloquentModel();
$model->setConnection('connection_name');
$result = $model->where($this->model->getKeyName(),'=',1)->get()->last();
@mfrancois You're correct. However, this is the way to query on the instantiated model:
$model->newQuery()->find(1);
Sign in to participate in this thread!