Laravel - get last row using whereHas(Laravel - 使用 whereHas 获取最后一行)
问题描述
我正在尝试获取最后一次用户活动的时间created_at",我有模型 User
和 UserActivity
.
我想获取最后一个用户活动并检查该用户的最后一个活动是否是 3 天才能发送通知,
I am trying to get the time "created_at" for the last user activity,
I have the model User
, and UserActivity
.
I want to get the last user activity and check if the last activity of this user is 3 days to send notification,
User.php
Activity.php
控制器
推荐答案
首先,在 User
模型中创建另一个关系,即 必须是 hasOne
以获取最新活动:
First, create another relationship in User
model, which has to be hasOne
to get the latest activity:
然后只加载最近活动超过 3 天的用户
Then load just users who have latest activities older than 3 days
这篇关于Laravel - 使用 whereHas 获取最后一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!