I'm trying to write a DB Query with Laravel 5.3's Has Many Through option
My DB Tables are layed out as follows :
http://sqlfiddle.com/#!9/a9c9af
The tables are :
companies company_offers offers
In my Model I have the following setup :
public function offers()
{
return $this->hasManyThrough(
'App\Offers', 'App\CompanyOffers',
'company_offer_id', 'offer_id'
);
}
When I try to run the following line of code :
return $this->whereHas('offers', function($query) use ($offer_id)
{
return $query->whereIn('offer_id', $offer_id);
});
I get the Error :
Integrity constraint violation: 1052 Column 'offer_id' in where clause is ambiguous
How do I go about correcting this and getting the relationship right?
I need to get the offer title & offer icon for my frontend view.
Hope this makes sense and someone can help me.
Thanks
"Column 'offer_id' in where clause is ambiguous" basically means the SQL query is dealing with several tables and it doesn't know which table offer_id
is referring to:
For example the query could be doing something like
select offers.* from offers
left join another_table on another_table.field = offers.field
where offer_id = ?
Notice that offer_id
in the above table could refer to a column in offers
or another_table
.
What you should do to correct the error is prefix the offer_id
with a table name. Something like this:
return $this->whereHas('offers', function($query) use ($offer_id) {
return $query->whereIn('other_table.offer_id', $offer_id);
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.