我正在尝试获取上次用户活动的时间“created_at”,
我有模型User
, and UserActivity
.
我想获取最后一个用户活动并检查该用户的最后一个活动是否是 3 天发送通知,
User.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class User extends Model
{
public function activites()
{
return $this->hasMany(Activty::class);
}
}
活动.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Carbon\Carbon;
class Activity extends Model
{
function function user(){
return $this->belongsTo(User::class);
}
}
控制器
$latest_activites = User::whereHas("activites",function($query){
$query->where("created_at",">=",Carbon::now()->subDays(3));
});
$latest_activites = $latest_activites->get();
您应该同时预加载用户活动,然后使用基于created_at的子查询过滤器。拨打最新电话以获取最后一条记录,您应该可以开始了。
$latest_activites = User::with('activites')
->where("activites",function($query){
$query->where("created_at",">=",Carbon::now()->subDays(3));
})->latest()->get();
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)