php - Laravel 关系不适用于 leftJoin
问题描述
我在 Laravel 中有这个标准(NearCreteria),
public function apply($model, RepositoryInterface $repository)
{
if ($this->request->has(['zone'])) {
$zone = $this->request->get('zone');
return $model
->leftJoin('zones_delivery', 'restaurants.zone_id', '=', 'zones_delivery.primary_zone')
->where("zones_delivery.secondary_zone", $zone)
->orderBy('closed');
} else {
return $model->orderBy('closed');
}
}
当我发送请求参数“zone”时,我得到了正确的结果,但是当我尝试获得“with”参数或任何其他关系的结果时,我总是得到空数组。
例如:
http://.../api/restaurants?with=galleries
返回每家餐厅,并按要求填充其画廊数组
http://.../api/restaurants?zone=2&with=galleries
返回具有空画廊但正确区域 = 2 的每家餐厅。
表关系:restaurants.zone_id => zone.id => zone_delivery.secondary_zone_id gallery.restaurant_id => restaurant.id
餐厅索引
public function index(Request $request)
{
try {
$this->restaurantRepository->pushCriteria(new RequestCriteria($request));
$this->restaurantRepository->pushCriteria(new LimitOffsetCriteria($request));
$this->restaurantRepository->pushCriteria(new RestaurantsOfCuisinesCriteria($request));
if ($request->has('popular')) {
$this->restaurantRepository->pushCriteria(new PopularCriteria($request));
} else {
$this->restaurantRepository->pushCriteria(new NearCriteria($request));
}
$this->restaurantRepository->pushCriteria(new ActiveCriteria());
$restaurants = $this->restaurantRepository->all();
} catch (RepositoryException $e) {
return $this->sendError($e->getMessage());
}
return $this->sendResponse($restaurants->toArray(), 'Restaurants retrieved successfully');
}
解决方案
推荐阅读
- java - Java 8 Swing 中是否有列表 gui 组件?
- python - 我可以在变量名中添加字符串吗?
- python - python 从 subprocess.PIPE 读取所有可用数据但不阻塞
- ios - 如何以 webview ios xamarin 形式访问摄像头和麦克风?
- sql - Oracle 中的员工经理层次结构查询
- java - 将独立 Java 应用程序从 Java 8 迁移到 JDK11 时出现 NoClassDefFoundError
- tensorflow - 使用 tf.Keras.Sequential API 和 LSTM 评估模型时激活经常性 dropout
- python - 如何在 discord.py 中使用循环更改状态?
- angular - RXJS:管道在条件下可观察
- javascript - 如何在对象数组中查找元素的所有出现并在Javascript中对它们的对应值求和