Laravel 4: Adding where clause to a join condition - php

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 Illuminate\Database\Query\JoinClause::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();

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

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

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

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

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

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.

Add this just before the get() function call
->where('contacts.effective_date', '=', $current_date);

$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/queries please check this link

Related

How to use leftJoin in Laravel for case?

I have the following code:
$registrations = User::where(function ($query) use ($request) {
if (!is_null($request->region) && $request->region !== '0') {
$query->where('region', $request->region);
}
})->get();
How to join User result with another model Clients with Left Join condition?
I tried this way:
$registrations = User->join('clients', 'clients.id', '=', 'user.id')->where(...
Read the documentation first: https://laravel.com/docs/5.5/queries#joins.
$registrations = User::where(....)
->join('clients', 'clients.id', '=', 'user.id')
->get();
You could catch it by reading docs
$users = DB::table('users')
->leftJoin('clients', 'users.id', '=', 'clients.user_id')
->where('','')
->get();

Join in Laravel 5.1

I'm new to Laravel and I'm still working on my Project using this Framework. In my project, I use an SQL JOIN but I don't know how to retrieve/fetch data on the joined table.
Can anybody help me on this one?
I really appreciate your help. Thanks!
Here is a simple example!
$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();
$users = \DB::table('users')
->join('contacts', 'contacts.user_id', '=', 'users.id')
->join('orders', 'orders.user_id', '=', 'users.id')
->select('users.id as id', 'contacts.phone as phone', 'orders.price as price')
//Where condition here if exist
->get();
$users will now have the array of values.Loop it in an array if necessary.
Eg:
forearch($users as $user)
{ $user->id;
$user->phone;
}
Like that.

orderBy is not working in Laravel 5

I am using the below query. orderBy is not working in below query. This query is working in localhost but it is not working in Online Server.
return DB::table('reports')
->leftJoin('sources', 'reports.report_source_id', '=', 'sources.id')
->select('*')
->orderBy('report_id', 'desc')
->take(10)
->get();
Try setting an alias for each table and then using the required alias on the orderBy
If there is a report_id in both tables it will not know which one to use and is probably throwing an error if you look for it.
return DB::table('reports as r')
->leftJoin('sources as s', 'r.report_source_id', '=', 's.id')
->select('*')
->orderBy('r.report_id', 'desc')
->take(10)
->get();
Try this..
use "reports.report_id";
return DB::table('reports')
->leftJoin('sources', 'reports.report_source_id', '=', 'sources.id')
->select('*')
->orderBy('reports.report_id', 'desc')
->take(10)
->all();
Try to use reports.report_id like
return DB::table('reports')
->leftJoin('sources', 'reports.report_source_id', '=', 'sources.id')
->select('*')
->orderBy('reports.report_id', 'desc')
->take(10)
->get();

Laravel/Eloquent query going wrong

I have two tables, one with users, one with the name of their document. The first table consists of two columns: id and username. The second one consists of three columns: id, userid and document_name.
Now, I'm trying to create a query in the controller. What should happen, ideally, is that if someone visits website.com/{documentname}, it displays the username of the owner. Also, this should only happen if the current logged in user is the owner of the document. However, this is proving more difficult than I imagined. As in, I can't see what I'm doing wrong.
Here's the query:
$user = DB::table('documents')
->join('users', function($join)
{
$join->on('users.id', '=', 'documents.userid')
->where('documents.userid', '=', Auth::id())
->where('documents.document_name', '=', $document_name);
})
->get();
**Try this query :**
$user = DB::table('documents')
->leftJoin('users', 'users.id', '=', 'documents.userid')
->where('documents.userid', '=', Auth::id())
->where('documents.document_name', '=', $document_name);
->get();
$document_name isn't in scope for the join function: you need to pass it through to the closure via use
$user = DB::table('documents')
->join('users', function($join) use ($document_name)
{
$join->on('users.id', '=', 'documents.userid')
->where('documents.userid', '=', Auth::id())
->where('documents.document_name', '=', $document_name);
})
->get();
EDIT
Because the WHERE conditions apply to the base table, and not to the JOIN:
$user = DB::table('documents')
->join('users', function($join) {
$join->on('users.id', '=', 'documents.userid')
})
->where('userid', '=', Auth::id())
->where('document_name', '=', $document_name);
->get();

Perform JOIN on three or four different tables in Laravel syntax

TABLES X(ID,A_ID)A(ID,B_ID),B(ID,C_ID),C(ID,D_ID)D(id,VALUE)
I want to retrieve the value of D table using laravel syntax on basis of X table Id and perform a JOIN with other tables.
Please post answers only in laravel syntax. In other format I can do. I am new to it so.
X::select('value')
->join('a', 'X.a_id', '=', 'a.id')
->join('b', 'a.b_id', '=', 'b.id')
->join('c', 'b.c_id', '=', 'c.id')
->join('d', 'c.d_id', '=', 'd.id')
->where('x.id', '=', $val)
->get();
But it is not working. please provide me proper solution. Right now I am using PHP logic to get the value, rather than optimise the query.
There are many ways, but this is the basic:
$rows = DB::table('shares')
->join('users', 'users.id', '=', 'shares.user_id')
->join('follows', 'follows.user_id', '=', 'users.id')
->where('follows.follower_id', '=', 3)
->get();
doc
Try this:
DB::table('X')
->join('a', 'X.a_id', '=', 'a.id')
->join('b', 'a.b_id', '=', 'b.id')
->join('c', 'b.c_id', '=', 'c.id')
->join('d', 'c.d_id', '=', 'd.id')
->select('D.value')
->where('x.id', '=', $val)
->get();
$result = X::select('a.value', 'd.*')
->join('a', 'x.a_id', '=', 'a.id')
->join('b', 'a.b_id', '=', 'b.id')
->join('c', 'b.c_id', '=', 'c.id')
->join('d', 'c.d_id', '=', 'd.id')
->where('x.id', '=', $val)
->get();
foreach ($result as $row) {
# code...
}

Categories