Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Query select() issue when using distance column #10

Closed
leabdalla opened this issue Sep 5, 2018 · 7 comments
Closed

Query select() issue when using distance column #10

leabdalla opened this issue Sep 5, 2018 · 7 comments

Comments

@leabdalla
Copy link

I need only these fields in my query:

$places->select(
    'id',
    'title',
    'subtitle',
    'distance'
);

This is working fine so far, but breaks when I add distance columns

$places->distance($params->latitude, $params->longitude);
$places->orderBy('distance', 'ASC');
$places->having('distance', '<=', $params->distance);

Adding this code it looks like my previous select() is ignored cause it returns all Model fields.

So I tried to put select() after distance and then it returns column error:

Column not found: 1054 Unknown column 'distance' in 'field list'

Am I doing something wrong?

@ashnehete
Copy link
Contributor

The underlying problem of this issue is that the distance method overrides the select() function of the model. I'm working on trying on a work around.

@ashnehete
Copy link
Contributor

I have opened a pull request that will be able to do exactly what you want. Few points though:

  1. select() should always be called before distance()/geofence().
  2. Don't include distance in the select column.

@malhal
Copy link
Owner

malhal commented Oct 7, 2018

When I first coded this selectRaw didn’t override any previous selects, see:
laravel/framework#8160
Does selectRaw now clear previous selects?

@ashnehete
Copy link
Contributor

The selectRaw doesn't override, thats correct.
But this line did that:

$query->select($this->getTable() . '.*');

@malhal
Copy link
Owner

malhal commented Oct 7, 2018

Hmm wonder why it has that...how about just addSelect the lat and lon columns?

@ashnehete
Copy link
Contributor

ashnehete commented Oct 7, 2018

According to the original issue, the author required arbitrary columns. So I felt best way was to give user the optional control to select any columns they need using original Laravel select(). Which I think makes it intutive for all users.

Edit: To answer you question, I think that line was there so that by default all the columns are selected. In the absence of that line only, the distance would have been selected.

@waqas306
Copy link

I have managed to select some required columns by just commenting the line
// $query->select($this->getTable() . '.*');
in scopeDistance function inside Geographical trait.

public function scopeDistance($query, $latitude, $longitude)
{
$latName = $this->getQualifiedLatitudeColumn();
$lonName = $this->getQualifiedLongitudeColumn();
// $query->select($this->getTable() . '.*');

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants