Quantcast
Channel: database – Tech ABC to XYZ
Viewing all articles
Browse latest Browse all 31

Laravel 4: Adding where clause to a join condition [ANSWERED]

$
0
0

Laravel 4: Adding where clause to a join condition

Asked By: user9507
Originally Asked On: 2014-01-02 09:48:21
Asked Via: stackoverflow

It says in the laravel docs that 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 IlluminateDatabaseQueryJoinClause::where(). Anyone knows how to add where clause in a join clause?

Laravel Website Example:

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

He received 6 answers
eventually accepting:

‘s answer to

Laravel 4: Adding where clause to a join condition

The answer with the highest score with 4 points was:

Pars’s answer to

Laravel 4: Adding where clause to a join condition

if you want add more condition on a join add more $join->on or $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 where inside your join closure, but since Laravel 4.1 and above you can have where conditions after your join condition. I couldn’t find documentation for Laravel 4.1 but this is the #join documentation for L4.2 and above

If the selected answer did not help you out, the other answers might!

All Answers For: Laravel 4: Adding where clause to a join condition

Christian’s answer to

Laravel 4: Adding where clause to a join condition

You are calling $current_date but you 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 😉

Pars’s answer to

Laravel 4: Adding where clause to a join condition

if you want add more condition on a join add more $join->on or $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 where inside your join closure, but since Laravel 4.1 and above you can have where conditions after your join condition. I couldn’t find documentation for Laravel 4.1 but this is the #join documentation for L4.2 and above

Amit’s answer to

Laravel 4: Adding where clause to a join condition

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

user3213246’s answer to

Laravel 4: Adding where clause to a join condition

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.

shashik493’s answer to

Laravel 4: Adding where clause to a join condition

Please Check Below Answer

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

Salma Omar’s answer to

Laravel 4: Adding where clause to a join condition

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

Of course, you should really check out the original question.

The post Laravel 4: Adding where clause to a join condition [ANSWERED] appeared first on Tech ABC to XYZ.


Viewing all articles
Browse latest Browse all 31

Trending Articles