如何使用“拥有”分页在关系栏中的laravel 5 [英] How to use 'having' with paginate on relationship's column in laravel 5

查看:135
本文介绍了如何使用“拥有”分页在关系栏中的laravel 5的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要抓住经销商距离关系的车辆, 200

  Vehicle :: join('dealers','vehicles.dealer_id','=','dealers.id') 
- > select(DB :: raw(dealers.id,(cos(弧度(纬度))* cos(弧度(经度)))AS距离))
- > havingRaw '距离<200');

我正在尝试使用havingRaw与关系(belongsTo)经销商的别名距离。但是失败了一个错误:


没有找到列:1054'having子句'中的未知列'distance'


更新



当我将分页功能添加到

  $ vehicle = Vehicle :: join('dealers','vehicles.dealer_id','=' ,dealers.id)
- > select(DB :: raw(dealers.id,(cos(弧度(纬度))* cos(弧度(经度)))AS距离))
- > havingRaw('distance< 200');

$ result = $ vehicle-> paginate(15);


解决方案

em>



问题是查询构建器,因为所有选择在执行聚合调用时都会被丢弃(如count(*))。 make-do解决方案目前是手动构建分页符:

  $ query = Vehicle :: join('经销商'''''''dealers.id')
- > select(DB :: raw(dealers.id,(cos(弧度(纬度))* cos(弧度(经度)))AS距离))
- 具有('distance','&','200');

$ perPage = 10;
$ curPage = \Illuminate\Pagination\Paginator :: resolveCurrentPage();

$ itemQuery = clone $ query;

$ items = $ itemQuery-> forPage($ curPage,$ perPage) - > get();

$ totalResult = $ query-> addSelect(DB :: raw('count(*)as count')) - > get();
$ totalItems = $ totalResult-> first() - > count;

$ vehicles = new \Illuminate\Pagination\LengthAwarePaginator($ items-> all(),$ totalItems,$ perPage);


I need to grab the vehicles whose relation 'dealer' is having distance < 200

Vehicle::join('dealers', 'vehicles.dealer_id', '=', 'dealers.id')
     ->select(DB::raw("dealers.id, ( cos( radians(latitude) ) * cos( radians( longitude ) ) ) AS distance"))
     ->havingRaw('distance < 200');

I am trying to use havingRaw on the alias 'distance' from the relation (belongsTo) dealer. But failed with an error:

Column not found: 1054 Unknown column 'distance' in 'having clause'

UPDATE

The issue actually occurs when I add paginate function to the above query like this.

$vehicle = Vehicle::join('dealers', 'vehicles.dealer_id', '=', 'dealers.id')
 ->select(DB::raw("dealers.id, ( cos( radians(latitude) ) * cos( radians( longitude ) ) ) AS distance"))
 ->havingRaw('distance < 200');

$result = $vehicle->paginate(15);

解决方案

Answer Updated corresponding to updated question

The problem is with the query builder as all selects are discarded when doing an aggregate call (like count(*)). The make-do solution, for now, is to construct the paginator manually as:

$query = Vehicle::join('dealers', 'vehicles.dealer_id', '=', 'dealers.id')
             ->select(DB::raw("dealers.id, ( cos( radians(latitude) ) * cos( radians( longitude ) ) ) AS distance"))
             ->having('distance', '<', '200');

$perPage = 10;
$curPage = \Illuminate\Pagination\Paginator::resolveCurrentPage();

$itemQuery = clone $query;

$items = $itemQuery->forPage($curPage, $perPage)->get();

$totalResult = $query->addSelect(DB::raw('count(*) as count'))->get();
$totalItems = $totalResult->first()->count;

$vehicles = new \Illuminate\Pagination\LengthAwarePaginator($items->all(), $totalItems, $perPage);

这篇关于如何使用“拥有”分页在关系栏中的laravel 5的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆