我正在使用Laravel并且需要从Model/Repository类运行原始sql查询
INSERT INTO calendar
(room_id, date, default_count, default_price)
VALUES ('1', '2017-01-02', '2', '400004')
ON DUPLICATE KEY UPDATE
default_count = VALUES(default_count), default_price = VALUES(default_price);
Run Code Online (Sandbox Code Playgroud)
恩.当我从UserRepository插入数据时
$this->users->insert(['email' => 'john@example.com', 'votes' => 0]);
Run Code Online (Sandbox Code Playgroud)
我需要某种方法来获取数据库连接并通过模型运行sql
//Something like
$this->users->execute($sql);
Run Code Online (Sandbox Code Playgroud)
我看到Laravel有updateOrInsert()
方法,但我需要一次为多个数据集运行它.
如何通过模型类或存储库运行原始sql查询?
谢谢
更新 - 已解决
我浏览了Eloquent Model Sourcecode,发现我可以从getConnection()方法获得连接
$this->users->getConnection()->statement($sql);
Run Code Online (Sandbox Code Playgroud)