Нашел ответ на
stackoverflow.com/questions/29069101/multi-tenancy...The problem is that setConnection() works on an instance of a class, but the create() method is a static method on the class itself. In your repository, $this->product is an instance of the Product class. Using setConnection() on this instance before doing queries will work fine, but you'll need to do a little more manual work if you want to use separate connections on the static methods (such as create()).
All the create() method does is instantiate a new instance with the given attributes and then call save(). So, instead of calling create() on the Product model, you'll just need to do this manually.
class ProductRepository {
public function create(array $attributes, $connection = null) {
$product = $this->product->newInstance($attributes);
$product->setConnection($connection ?: $this->product->getConnectionName());
$product->save();
return $product;
}
}
You could also override the static create() method on the Product model to accept a connection, as well.
class Product extends Model {
public static function create(array $attributes, $connection = null) {
$model = new static($attributes);
$model->setConnection($connection ?: $this->connection);
$model->save();
return $model;
}
}
class ProductRepository {
public function create(array $attributes, $connection = null) {
$connection = $connection ?: $this->product->getConnectionName()
return $this->product->create($attributes, $connection);
}
}