mysql - 在原始 mysql 中编写 Eloquent 查询
问题描述
我想知道如何将这两个 Eloquent 查询写入原始 SQL?还有有没有办法将 Eloquent 系列转换为原始 SQL 系列以便我可以使用它们?
public function items()
{
return $this->belongsToMany(Product::class, 'order_items', 'order_id', 'product_id')->withPivot('quantity', 'price');
}
public function user()
{
return $this->belongsTo(User::class);
}
解决方案
如果我们谈论第一个查询中使用的items
方法。将其更改为原始 SQL 后,它将变为
DB::select('select `products`.*, `order_items`.`order_id` as `pivot_order_id`, `order_items`.`product_id` as `pivot_product_id`, `order_items`.`quantity` as `pivot_quantity`, `order_items`.`price` as `pivot_price` from `products` inner join `order_items` on `products`.`id` = `order_items`.`product_id` where `order_items`.`order_id` = `order_id`');
并且方法内的第二个查询user
将变为
DB::select('select * from `users` where `users`.`id` = `id`');
推荐阅读
- python - 当它应该在范围内时,列出超出范围的索引
- angular - IE11 中的 Angular 7 和材质渲染不正确
- python - Python 执行另一个文件
- python - Python Sympy求解器在求解三角方程时出错
- python - 重新启用 spacy 的解析器组件给出错误
- python - 如何有效地执行字典合并?
- python-3.x - 功能外观,需要生成一个随机选择 4 个选项的列表
- angular - 运行业力单元测试用例“引发未知 [对象对象]”时出错
- python - 我的时间序列图如何从“00:00”开始并在“23:00”结束?两边没有空格
- asp.net-mvc - 在 Bamboo 中设置 NSIS 构建