Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/282.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Laravel添加要联接的函数_Php_Laravel - Fatal编程技术网

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();
}