【发布时间】:2021-02-10 03:40:43
【问题描述】:
如何根据与 table2 的连接对 table1 中的产品进行排序,即通过 table2 中的 example_relation 对 table1 中的产品进行排序?
表1
| id | product |
|----|---------|
| 1 | pro1 |
| 2 | pro2 |
| 3 | pro3 |
| 4 | pro4 |
| 5 | pro5 |
表2
| id | example_relation | product_id |
|----|------------------|------------|
| 1 | 700 | 1 |
| 2 | 800 | 2 |
| 3 | 900 | 3 |
| 4 | 850 | 2 |
| 5 | 600 | 4 |
| 6 | 125 | 5 |
Table1 模型页面:
public $belongsTo = [
'pro_relation' => [
'models\Model',
'key' => 'id',
'otherKey' => 'product_id',
],
需要正确排列的排序部分:
$query->whereHas('pro_relation', function($q) use ($postFrom,$postTo){
$q->select('example_relation END AS expose');
})->orderBy("expose", DESC);
【问题讨论】:
-
你想按什么排序?
-
@AnuratChapanond 是的,我想使用 order by
-
但是你想要的结果到底是什么?按 example_relation 字段排序?
-
@AnuratChapanond 是的,我想按 example_relation 对产品进行排序。
标签: laravel octobercms