Php Laravel添加要联接的函数
我有两个表,一个产品表和一个用户表 在“我的用户”表中,有一个Php Laravel添加要联接的函数,php,laravel,Php,Laravel,我有两个表,一个产品表和一个用户表 在“我的用户”表中,有一个last\u login列,它返回日期时间 在这个查询中,我希望能够创建一个函数,该函数只允许用户在一定时间内未联机时获取产品 我曾经考虑过使用连接,但是我对它们不太熟悉 类似于 $products = Product::where("price", "<=", $maxBudget) ->where('active', 1) ... ->join('users
last\u login
列,它返回日期时间
在这个查询中,我希望能够创建一个函数,该函数只允许用户在一定时间内未联机时获取产品
我曾经考虑过使用连接,但是我对它们不太熟悉
类似于
$products = Product::where("price", "<=", $maxBudget)
->where('active', 1)
...
->join('users', function ($join) {
$join->on('products.created_by', '=', 'users.id')
->where('last_login', '>', 2592000);
})
->get()
我该怎么做呢?如果您试图加入表,那么您应该能够执行以下操作:
// min seconds
$threshold = 123456;
Product::query()
->join('users', 'products.created_by', '=', 'users.id')
->where('products.active', 1)
->where('users.last_login', '<=', now()->subSeconds($threshold)->toDateTimeString())
->select(['products.*', 'users.last_login'])
->get();
// min seconds
$threshold = 123456;
Product::query()
->join('users', 'products.created_by', '=', 'users.id')
->where('products.active', 1)
->where('users.last_login', '<=', now()->subSeconds($threshold)->toDateTimeString())
->select(['products.*', 'users.last_login'])
->get();
// Get the user's last login attribute.
$lastLogin = auth()->user()->last_login;
// whatever the minimum time since their last login should be.
$threshold = 12345;
$exceeded = now()->diffInSeconds(Carbon::parse($lastLogin))->gte($threshold);
if ($exceeded) {
Product::where(...)->get();
}