它在 laravel docs 中说可以在连接上添加 where 子句,但是每当我在代码中尝试使用 where 子句时,我都会收到错误消息:Call to undefined method Illuminate\Database\Query\JoinClause::where()
。谁知道如何在连接子句中添加 where 子句?
Laravel 网站示例:
DB::table('users')
->join('contacts', function($join)
{
$join->on('users.id', '=', 'contacts.user_id')
->where('contacts.user_id', '>', 5);
})
->get();
我要实现的代码:
DB::table('users')
->join('contacts', function($join)
{
$current_date = date('Y-m-d');
$join->on('users.id', '=', 'contacts.user_id')
->where('contacts.effective_date', '>=', $current_date);
})
->get();
最佳答案
如果您想在 join
上添加更多条件,请添加更多 $join->on
或 $join->orOn
。
如果您想为第一个选择添加条件,请将其添加到连接函数之外。
DB::table('users')
->join('contacts', function($join)
{
$date = date('Y-m-d');
$join->on('users.id', '=', 'contacts.user_id');
})
->where('contacts.effective_date', '>=', $date);
->get();
已更新
在我认为你使用的 Laravel 4.0 中,你不能在 join 闭包中使用 where
,但由于 Laravel 4.1 及更高版本,你可以在 join 条件之后使用 where
条件。我找不到 Laravel 4.1 的文档,但是 this is the #join documentation for L4.2 and above
关于php - 拉维尔 4 : Adding where clause to a join condition,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20880151/