Skip to content

Instantly share code, notes, and snippets.

@TiagoSilvaPereira
Last active March 14, 2019 20:18
Show Gist options
  • Save TiagoSilvaPereira/80d4949ab83093a2b7ad429d1fefe269 to your computer and use it in GitHub Desktop.
Save TiagoSilvaPereira/80d4949ab83093a2b7ad429d1fefe269 to your computer and use it in GitHub Desktop.
KISS aplicado ao código
<?php
// Código complexo gerado pela falta de simplicidade na estrutura
$labels = DB::table('labelables')
->select(['labelables.*', 'labels.id as label_id', 'labels.slug', 'labels.title'])
->join('labels', 'labels.id', 'labelables.label_id')
->where('labels.title', 'like', '%'.$search.'%')
->where('labelables.labelable_type', $request->labelable_type)
->whereRaw('(SELECT count(id)
FROM labelables AS count_labelables
WHERE count_labelables.labelable_type = labelables.labelable_type
AND count_labelables.project_id = labelables.project_id
AND count_labelables.labelable_id = ?
AND count_labelables.label_id = labelables.label_id
AND count_labelables.color = labelables.color
AND count_labelables.deleted_at IS NULL) = 0', $request->labelable_id)
->groupBy(['labels.title', 'labelables.color'])
->orderBy('labelables.updated_at', 'ASC')
->limit($limit);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment