【发布时间】:2018-10-05 17:09:04
【问题描述】:
三个主表:
产品
广告商
地点
两个数据透视表:
广告商位置
products_locations
关系:
一个产品属于一个广告商,一个广告商有很多位置(可以将产品运送到的位置)
产品还可以拥有自己的一组位置,这些位置会覆盖广告客户的位置(某些产品有配送限制)
我需要做的是:
选择所有产品
检查 products_locations 表中的产品 ID 并加入它。
如果不存在则加入广告商位置表
这可以在一个查询中使用 eloquent 完成吗?这是我的代码 - 与条件作斗争:
public function scopeWhereShippableToLocation($query)
{
$location_id = session('location_id');
$query->where(function ($q) use ($location_id) {
$q->join('products_locations', 'products_locations.product_id', '=', 'products.id')
->where('products_locations.location_id', '=', $location_id);
});
$query->orWhere(function ($q) use ($location_id) {
$q->join('advertisers_locations', 'advertisers_locations.advertiser_id', '=', 'products.advertiser_id')
->where('advertisers_locations.location_id', '=', $location_id);
});
//dd($q->toSql());
return $query;
}
目前正在产生 MySQL 错误:
Column not found: 1054 Unknown column 'products_locations.location_id' in 'where clause' (SQL: select `products`.*,
【问题讨论】:
标签: php mysql laravel laravel-5