laravel multiple join conditions with integer values - mysql

I want to make a join with multiple conditions:
->join('filters', function($join) {
$join->on('filters.result_id', '=', 'results.id');
$join->on('filters.result_filter_id', '=', 2);
$join->on('filters.value', '>', 1);
})
This results in an error:
Unknown column '2' in 'on clause'
How can I use actual values in the join on condition without having eloquent interpret it as a column name? It works fine with the actual SQL query:
JOIN filters ON filters.result_id = results.id
AND filters.result_filter_id = 2
AND filters.value > 1

Replace on with where if you want to filter your join
If you would like to use a "where" style clause on your joins, you may use the where and orWhere methods on a join
->join('filters', function($join) {
$join->on('filters.result_id', '=', 'results.id')
->where('filters.result_filter_id', '=', 2)
->where('filters.value', '>', 1);
})
Documentation

Related

include rows that have column with null value on join in laravel [duplicate]

I have such SQL query, which works fine:
SELECT A.program_id FROM A
LEFT JOIN B
ON A.program_id = B.program_id
WHERE
B.program_id IS NULL
But i want it to rewrite into LARAVEL style, like this:
\DB::table('A')
->join('B', 'A.program_id', '=', 'B.program_id')
->select('A.program_id')
->whereNull('B.program_id')
->get()->toArray();
But this code returns me 0 results.
You are using join instead of left join
Try this
\DB::table('A')
->leftjoin('B', 'A.program_id', '=', 'B.program_id')
->select('A.program_id')
->whereNull('B.program_id')
->where('A.student_id', '=', 5)
->get()->toArray();
It will produce a query like

select a specfic column from a inner joined table in laravel

I need all columns from invoice,invoice_details table but I want to get only one column (column name: amount) from my payment table. What will be my query for it? here is my controller.
$all_invoice = DB::table('invoice')
->join('invoice_details', 'invoice.invoice_id', '=', 'invoice_details.invoice_id')
->join('payment', 'invoice.invoice_id', '=', 'payment.invoice_id')
->where('invoice.client_id', 7)
->orderBy('invoice.invoice_id', 'DESC')
->get();
This very simple you need to add select clause in query like this.
$all_invoice = DB::table('invoice')->select(['invoice.*', 'payment.amount'])
->join('invoice_details','invoice.invoice_id','=','invoice_details.invoice_id')
->join('payment','invoice.invoice_id','=','payment.invoice_id')->where('invoice.client_id',7)->orderBy('invoice.invoice_id','DESC')->get();
If have still query the see the laravel docs in select option in query.
Just using select
$all_invoice = DB::table('invoice')
->join('invoice_details', 'invoice.invoice_id','=','invoice_details.invoice_id')
->join('payment', 'invoice.invoice_id', '=', 'payment.invoice_id')
->where('invoice.client_id', 7)
->select('payment.amount')
->orderBy('invoice.invoice_id', 'DESC')
->get();

How to select max aggregate within a subquery with a join in eloquent

I want to select the most recent entry but there are no timestamps(created_at) it can be identified by selecting the max on the primary key. The problem is to this within a subquery and tables needs to be joined.
The query is like:
$prodarr = DB::table('onhand_inventory')
->leftJoin('mapping', 'onhand_inventory.sku', '=', 'mapping.sku')
->where('mapping.mid', function ($query) {
$query->select('mapping.mid')->from('mapping')
->whereRaw('onhand_inventory.sku = mapping.sku')
->max('mapping.mid');
})->leftJoin('order_products', 'order_products.sku', '=', 'onhand_inventory.sku')
->leftJoin('vendors', 'mapping.v_id', '=', 'vendors.vid')
->where('onhand_inventory.shipped_with_box', 0)
->groupBy('onhand_inventory.inv_id')
->orderBy('onhand_inventory.inv_id')
->get();
On executing the above query following error is thrown
Column not found: 1054 Unknown column 'onhand_inventory.sku' in 'where clause'
How to get the onhand_inventory's data to be compared with the mapping's inside the subquery.
You already joined the mapping table so you dont need the sub query.
$prodarr = DB::table('onhand_inventory')
->leftJoin('mapping', 'onhand_inventory.sku', '=', 'mapping.sku')
->leftJoin('order_products', 'order_products.sku', '=', 'onhand_inventory.sku')
->leftJoin('vendors', 'mapping.v_id', '=', 'vendors.vid')
->where('onhand_inventory.shipped_with_box', 0)
->orderBy('mapping.mid', 'desc')
->groupBy('onhand_inventory.inv_id')
->orderBy('onhand_inventory.inv_id')
->limit(1)
->get();
I didnt run the code but i hope this will help.

JOIN after WHERE clause in laravel

I am performing a query in which desirable output is first apply where clause then after I want to perform right join.I have this query but it perform first join then where.thank you for you any help advance.
Here is my query
$friends = friend_list::where('friend_lists.user_id', '=', $id)
->rightJoin('users as u1', 'u1.id', '=', 'friend_lists.user_id2')
->where('friend_lists.user_id2','!=',$id)
->where('u1.id','!=',$id)
->orwhere('status',null)
->get();
edited:
I am implementing friend request section.
In which user table contains...user(id,name,..,..) friend_list(userid,userid2,status)
in friend_list shows status with other friend.I do right join withfriend_list.if status is null then there is no row for that user with current user.so status is null and it shows add friend
.so null value is important therefor right join with user table
Change the WHERE to another JOIN condition
$friends = friend_list::rightJoin('users as u1', function($join) {
$join->on('u1.id', '=', 'friend_lists.user_id2')
->where('friend_lists.user_id', '=', $id);
})
->where('friend_lists.user_id2','!=',$id)
->where('u1.id','!=',$id)
->orwhere('status',null)
->get();

two syntax problems with join and left-join in the same query

I have this code to create a collection:
$rank_entities_by_capacity = Entity::join('entity_capacitytypes', function($q){
$q->on('entitys_id', '=', 'entities.id');
$q->where('capacitytypes_id','=', '23');
})->
leftJoin('user_attitudes', function($q){
$q->on('entity_id', '=', 'entities.id');
$q->where('item_type', '=', 'entity');
})
->selectRaw('entities.*, SUM(user_attitudes.importance) AS importance')
->groupBy('entities.id')
->orderBy('importance', 'desc')
->take(6)
->get();
First problem:
1052 Column 'entity_id' in on clause is ambiguous:
[2015-01-01 13:22:28] production.ERROR: FATAL DATABASE ERROR: 500 = SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'entity_id' in on clause is ambiguous (SQL: select entities.*, SUM(user_attitudes.importance) AS importance from entities inner join entity_capacitytypes on entity_id = entities.id and capacitytypes_id = 23 left join user_attitudes on entity_id = entities.id and item_type = entity group by entities.id order by importance desc limit 6) [] []
I traded over an hour for finding a trick to bypass the problem:
I was forced to change column name in table entity_capacitytypes from entity_id (it was causing problems) to entitys_id just in order to avoid this error.
Now my database names are not consistent. Any other way to avoid the error?
Problem 2:
If I add this part to the query, and try to use a previously perfectly working variable in the where line
join('entity_capacitytypes', function($q){
$q->on('entitys_id', '=', 'entities.id');
$q->where('capacitytypes_id','=', $capacity);
})->
I get this error:
Undefined variable: capacity
How make the variable work?
My solution: avoidance again.
I ould't fix join, so I used a relation defined in Capacitytype model:
public function entities()
{
return $this->belongsToMany('Entity', 'entity_capacitytypes', 'capacitytypes_id', 'entitys_id');
}
and instead using join, I accessed Entity from another model
$rank_entities_by_capacity = Capacitytype::find($capacity)->entities()->
leftJoin('user_attitudes', function($q){
$q->on('entity_id', '=', 'entities.id');
$q->where('item_type', '=', 'entity');
})
->selectRaw('entities.*, SUM(user_attitudes.importance) AS importance')
->groupBy('entities.id')
->orderBy('importance', 'desc')
->take(6)
->get();
To do:
make the variable work in join
Problem 1
To fix the "ambiguous column" problem you only need to specify the full column name including the table
entity_capacitytypes.entity_id instead of only entity_id
Problem 2
To use a local variable like $capacity inside a closure (aka anonymous function) you need to inject them with use
join('entity_capacitytypes', function($q) use ($capacity){
$q->on('entitys_id', '=', 'entities.id');
$q->where('capacitytypes_id','=', $capacity);
})