php Laravel 4:将 where 子句添加到连接条件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20880151/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 03:38:57  来源:igfitidea点击:

Laravel 4: Adding where clause to a join condition

phpdatabaselaravellaravel-4fluent

提问by user9507

It says in the laravel docsthat it is possible to add where clause on a join, but whenever I try in my code using the where clause, I get the error: Call to undefined method Illuminate\Database\Query\JoinClause::where(). Anyone knows how to add where clause in a join clause?

它在laravel 文档中说可以在连接上添加 where 子句,但是每当我尝试使用 where 子句在我的代码中时,我都会收到错误:Call to undefined method Illuminate\Database\Query\JoinClause::where()。任何人都知道如何在 join 子句中添加 where 子句?

Laravel Website Example:

Laravel 网站示例:

DB::table('users')
->join('contacts', function($join)
{
  $join->on('users.id', '=', 'contacts.user_id')
  ->where('contacts.user_id', '>', 5);
})
->get();

Code I'm trying to implement:

我正在尝试实现的代码:

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

回答by Pars

if you want add more condition on a joinadd more $join->onor $join->orOn.

如果您想在joinadd more$join->on$join->orOn.

if you want to add a condition to your first select, add it outside join function.

如果要在第一个选择中添加条件,请将其添加到连接函数之外。

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


Updated
In Laravel 4.0 which I think you use, you can't use whereinside your join closure, but since Laravel 4.1 and above you can have whereconditions after your join condition. I couldn't find documentation for Laravel 4.1 but this is the #join documentation for L4.2 and above


在我认为您使用的 Laravel 4.0 中更新,您不能where在连接闭包中使用,但由于 Laravel 4.1 及更高版本,您可以where在连接条件之后使用条件。我找不到 Laravel 4.1 的文档,但这是 L4.2 及更高版本的 #join 文档

回答by shashik493

Please Check Below Answer

请检查以下答案

DB::table('users')
        ->join('contacts', function($join)
        {
            $join->on('users.id', '=', 'contacts.user_id')
                 ->where('contacts.user_id', '>', 5);
        })
        ->get();

回答by Amit

Try This solution

试试这个解决方案

 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)
             ->where('contacts.effective_date', '=', $current_date);

            })
            ->get();

回答by user3213246

You are sure that you are working with laravel 4.1? I think you are using laravel 4.0 instead of 4.1. Look in your composer.json file.

您确定您正在使用 laravel 4.1 吗?我认为您使用的是 laravel 4.0 而不是 4.1。查看您的 composer.json 文件。

回答by Christian

You are calling $current_date but you decarle $date

你打电话给 $current_date 但你 decarle $date

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

I don't know if this solve the problem, try it ;)

我不知道这是否能解决问题,试试吧;)

回答by Salma Omar

$current_date = date('Y-m-d');
DB::table('users')
->join('contacts', function($join) use ($current_date)
{
  $join->on('users.id', '=', 'contacts.user_id')
      ->where('contacts.effective_date', '>=', $current_date);
})
->get();

回答by ashok

$users = DB::table('users')
    ->join('contacts', 'users.id', '=','contacts.user_id')
    ->join('orders', 'users.id', '=', 'orders.user_id')
    ->select('users.*', 'contacts.phone', 'orders.price')->get();

https://laravel.com/docs/5.6/queriesplease check this link

https://laravel.com/docs/5.6/queries请查看此链接