我有一个 laravel-medialibrary 将数据库记录保存到不正确的数据库连接的问题,似乎调用 addMedia() 方法总是连接到默认的 mysql 连接?
我已经搜索并且在文档中看不到任何引用,我能找到的唯一相关搜索是Laravel-medialibrary working with multiple databases,但是我在我的模态中使用静态方法动态设置数据库连接:
public static function connect($connection)
{
DB::purge($connection);
return (new static)->setConnection($connection);
}
在我的模型实例上转储$this->student->getConnectionName()
集时,它已正确设置为正确的连接。但是,添加如下媒体会保存到默认连接。
this->student
->addMedia($this->avatar->getRealPath())
->toMediaCollection('avatar');
$this->student 设置在哪里:
$this->student = Student::connect('school_' . $host->id)
->where('student_reference_code', $this->student_reference_code)
->first();
我假设 addMedia 必须返回模型的新静态实例,其中不会设置连接?如果我要查询一些雄辩的方法,例如:
$this->student = Student::connect('school_' . $host->id)->all()
因为 all() 返回以下内容:
return static::query()->get(
is_array($columns) ? $columns : func_get_args()
);
任何有关如何在正确的数据库连接中存储媒体的帮助将不胜感激。