php - 拉维尔 4 : Adding where clause to a join condition

标签 php database laravel laravel-4 fluent

它在 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/

相关文章:

PHP - 使用 cookie 加载 CSS,但未读取 cookie?

php - 在没有 15 英里半径的情况下定位最近的 lng/lat 位置的方法?

php - 在现有的 nginx PHP 配置中添加 Flask 应用程序

database - 我如何在播放框架中使用默认数据库以外的其他数据库?

php - Laravel 4 CMS Composer 模块?

javascript - 执行存储方法时出现 Ajax CRUD 错误

laravel - 验证匹配字符串

php - ajax 请求图像闪烁

php - Laravel 什么是使用 HasFactory?

database - 使用 begin..end 或 call 调用存储过程