Skip to content

Instantly share code, notes, and snippets.

@dougogodinho
Last active February 29, 2016 20:55
Show Gist options
  • Save dougogodinho/e75b42170a3bad051e19 to your computer and use it in GitHub Desktop.
Save dougogodinho/e75b42170a3bad051e19 to your computer and use it in GitHub Desktop.
<?php
class Bid {
// ....
public function anuncios()
{
$query = Anuncio::query()
->join('modelo_anos', 'modelo_anos.id', '=', 'modelo_ano_id')
->join('modelos', 'modelos.id', '=', 'modelo_id');
// range de ano
if ($this->ano_inicio) {
$query->where('ano', '>=', $this->ano_inicio);
}
if ($this->ano_fim) {
$query->where('ano', '<=', $this->ano_fim);
}
// range de preço
if ($this->preco_inicio) {
$query->where('preco', '>=', $this->preco_inicio);
}
if ($this->preco_fim) {
$query->where('preco', '<=', $this->preco_fim);
}
// distância máxima
// TODO: verificação de distancias entre os usuários
// filtro modelo
if ($this->modelo_id) {
$query->where('modelo_id', $this->modelo_id);
}
// filtro carroceria
if ($this->carroceria_id) {
$query->where('carroceria_id', $this->carroceria_id);
}
// filtro cores
if ($this->cores->count()) {
$query->whereIn('cor_id', $this->cores->fetch('cor_id'));
}
// filtro opcionais
$this->opcionais->each(function ($opcional) use ($query) {
$query->join('anuncio_opcionais AS aop' . $opcional->opcional_id,
'aop' . $opcional->opcional_id . '.anuncio_id', '=', 'anuncios.id');
$query->where('aop' . $opcional->opcional_id . '.opcional_id', $opcional->opcional_id);
});
return new ImplicitRelation($query, $this);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment