首页 > 解决方案 > 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');
}

标签: phpmysqllaraveleloquentrequest

解决方案


推荐阅读