拉维尔(Laravel)
我正在使用Laravel 8构建应用程序。此应用程序具有表顺序和表跟踪。订单和跟踪具有一对一关系,模型如下:
class Order extends Model
{
protected $with = ['tracking'];
public function tracking()
{
return $this->hasMany(Tracking::class);
}
}
class Tracking extends Model
{
public function order()
{
return $this->belongsTo(Order::class);
}
}
现在我想查询订单在最后插入的跟踪中通过状态过滤它们的订单。对于以下数据,对于出现:
Order
ID VALUE
1 100.00
2 200.00
3 300.00
Tracking
ID ORDER_ID STATUS CREATED_AT
1 1 Accept 2022-03-01 00:00:00
2 1 Paid 2022-03-02 00:00:00
3 2 Accept 2022-03-01 00:00:00
4 2 Paid 2022-03-02 00:00:00
5 2 Cancel 2022-03-03 00:00:00
6 3 Accept 2022-03-01 00:00:00
7 3 Paid 2022-03-02 00:00:00
如果子句中的param status status =已付费,我希望带有ID 1和3的返回订单,并且所有跟踪都相关。
我尝试使用这样的地方:
$query = Order::query();
$query->whereHas('tracking', function ($query) use ($request) {
return $query->where('status', '=', 'Paid');
});
$orders = $query->paginate(10);
但是,这在所有跟踪中都会进行查询,并且只有在最后一个状态付费时,我才需要查询。
我需要这样的数据:
Order ID VALUE TRACKING
1 100.00 [[ID: 1, STATUS: Accept], [ID: 2, STATUS: Paid]]
3 300.00 [[ID: 6, STATUS: Accept], [ID: 7, STATUS: Paid]]
请注意,ID 2的订单已在2022-03-02付款,但在2022-03-03-03 00:00:00取消了该订单,因此该订单已取消,因此不会出现。
欢迎任何帮助。
谢谢。
I’m build a app with laravel 8. This app has table order and table tracking. The order and tracking has a one to many relation, the models are like below:
class Order extends Model
{
protected $with = ['tracking'];
public function tracking()
{
return $this->hasMany(Tracking::class);
}
}
class Tracking extends Model
{
public function order()
{
return $this->belongsTo(Order::class);
}
}
Now I want query the orders filtering them by status in the last tracking inserted. For exemple, with the data below:
Order
ID VALUE
1 100.00
2 200.00
3 300.00
Tracking
ID ORDER_ID STATUS CREATED_AT
1 1 Accept 2022-03-01 00:00:00
2 1 Paid 2022-03-02 00:00:00
3 2 Accept 2022-03-01 00:00:00
4 2 Paid 2022-03-02 00:00:00
5 2 Cancel 2022-03-03 00:00:00
6 3 Accept 2022-03-01 00:00:00
7 3 Paid 2022-03-02 00:00:00
If the param in where clause is status = Paid, I want return order with id 1 and 3, with all tracking related.
I try to use whereHas like this:
$query = Order::query();
$query->whereHas('tracking', function ($query) use ($request) {
return $query->where('status', '=', 'Paid');
});
$orders = $query->paginate(10);
but this make a query in all tracking, and I need query only if the last status is like Paid.
And I need data like this:
Order ID VALUE TRACKING
1 100.00 [[ID: 1, STATUS: Accept], [ID: 2, STATUS: Paid]]
3 300.00 [[ID: 6, STATUS: Accept], [ID: 7, STATUS: Paid]]
Notice, that order with id 2 has Paid in 2022-03-02 but in 2022-03-03 00:00:00 its canceled, so this wont appear.
Any help is welcome.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我现在无法再现这种情况。因此,建议无需进行任何性能测试的解决方案。我希望它能解决您的问题。
I cannot reproduce the situation right now. So, suggesting the following solution without any performance testing. I hope it will solve your issue.
想要的结果有点复杂,因此我们需要注意我们将如何实现它。
为此,让我们将获取过程分解为各个部分:
order_id
和值
和tracking_id
,<代码>状态create_at
从相关跟踪
记录。这些相关的记录将使用内部加入
ontracking.ording.order_id = orders.id
。上获取这些相关记录
status ='付费'
过滤,因此在查询中,我们可能会在procking.status ='付费'中使用之类的东西。
获取我的订单,其中最新的跟踪状态为“已付费”
,这意味着如果特定订单>最新的
tracking.status
/code>不是'付费'
,我们不希望该顺序,也不希望其跟踪
s。跟踪
与order> order
相关的记录,并且如果该相关记录具有状态< /代码>等于
付费
然后获取该order
的所有相关记录。这是一个
sql
查询,当我们需要tracking.status ='付费'
:让我尝试将上述查询转换为Laravel的查询构建器:
基于问题中提供的数据样本,上述
sql
查询应返回:7 。
The wanted result is a bit complex to achieve, so we need to pay attention to how we'll achieve it.
To do so, let's decompose the fetching process into parts :
order_id
andvalue
fromorders
table.tracking_id
,status
andcreated_at
from the relatedtracking
records. Those related records will be fetched using anINNER JOIN
clause ontracking.order_id = orders.id
.status = 'paid'
so in the query we'll likely to have something likewhere tracking.status = 'paid'
.GET ME orders WHERE LATEST TRACKING STATUS IS 'paid'
which means if the latesttracking.status
for a specificorder
is not'paid'
then we don't want that order, nor itstracking
s, at all.tracking
record related toorder
and if that related record has astatus
equals topaid
then fetch all the related records for thatorder
.Here's an
SQL
query that fetches the wanted results when we needtracking.status = 'paid'
:Let me try to translate the above query into Laravel's query builder:
Based on the data sample provided in the question, the above
SQL
query should return :Hope i have pushed you further.
编辑:我以前的答案是完全错误的,所以这是另一种尝试:
更轻松的方法可能是在订单模型中定义扩展的关系:
那么,您可以使用Active的订单来获取订单跟踪记录:
Edit: My previous answer was totally wrong, so here's another attempt:
An easier way to do this probably would be to define an extended relationship in the Order model:
Then you can just fetch your orders with with active Tracking records:
更新1.2
根据我的评论 ,我已更新了答案。我也是一个好主意,如果
订单
的最后一个track
没有取消
值,我将返回此曲目,我不会退货。Update 1.2
According to my comment, I have updated my answer. I've a nice idea also is that if the last
track
of theorder
has not has aCancel
value I'll return this track else, I'll not return it.