Skip to content

Commit

Permalink
Add the ability to filter components by tags. Closes cachethq#2920
Browse files Browse the repository at this point in the history
  • Loading branch information
jbrooksuk authored and liushuyu committed Oct 2, 2018
1 parent 8ebdde8 commit c12ab3f
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 11 deletions.
4 changes: 4 additions & 0 deletions app/Http/Controllers/Api/ComponentController.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ public function index()
$components = Component::enabled();
}

if ($tags = Binput::get('tags')) {
$components->withAnyTags($tags);
}

$components->search(Binput::except(['sort', 'order', 'per_page']));

if ($sortBy = Binput::get('sort')) {
Expand Down
4 changes: 2 additions & 2 deletions app/Models/Component.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
use AltThree\Validator\ValidatingTrait;
use CachetHQ\Cachet\Models\Traits\SearchableTrait;
use CachetHQ\Cachet\Models\Traits\SortableTrait;
use CachetHQ\Cachet\Models\Traits\Taggable;
use CachetHQ\Cachet\Models\Traits\HasTags;
use CachetHQ\Cachet\Presenters\ComponentPresenter;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
Expand All @@ -23,7 +23,7 @@

class Component extends Model implements HasPresenter
{
use SearchableTrait, SoftDeletes, SortableTrait, Taggable, ValidatingTrait;
use HasTags, SearchableTrait, SoftDeletes, SortableTrait, ValidatingTrait;

/**
* List of attributes that have default values.
Expand Down
4 changes: 2 additions & 2 deletions app/Models/Incident.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
use AltThree\Validator\ValidatingTrait;
use CachetHQ\Cachet\Models\Traits\SearchableTrait;
use CachetHQ\Cachet\Models\Traits\SortableTrait;
use CachetHQ\Cachet\Models\Traits\Taggable;
use CachetHQ\Cachet\Models\Traits\HasTags;
use CachetHQ\Cachet\Presenters\IncidentPresenter;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
Expand All @@ -30,7 +30,7 @@
*/
class Incident extends Model implements HasPresenter
{
use SearchableTrait, SoftDeletes, SortableTrait, Taggable, ValidatingTrait;
use HasTags, SearchableTrait, SoftDeletes, SortableTrait, ValidatingTrait;

/**
* Status for incident being investigated.
Expand Down
11 changes: 4 additions & 7 deletions app/Models/Traits/Taggable.php → app/Models/Traits/HasTags.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,15 @@
namespace CachetHQ\Cachet\Models\Traits;

use CachetHQ\Cachet\Models\Tag;
use CachetHQ\Cachet\Models\Taggable as TaggableModel;
use Illuminate\Database\Eloquent\Builder;
use InvalidArgumentException;

/**
* This is the taggable trait.
* This is the has tags trait.
*
* @author James Brooks <[email protected]>
*/
trait Taggable
trait HasTags
{
/**
* Get the tags relation.
Expand All @@ -43,7 +42,7 @@ public function scopeWithAllTags(Builder $query, $tags)
{
$tags = static::convertToTags($tags);

return $tags->each(function ($tag) use ($query) {
$tags->each(function ($tag) use ($query) {
$query->whereHas('tags', function (Builder $query) use ($tag) {
return $query->where('id', $tag ? $tag->id : 0);
});
Expand All @@ -65,9 +64,7 @@ public function scopeWithAnyTags(Builder $query, $tags)
return $query->whereHas('tags', function (Builder $query) use ($tags) {
$tagIds = $tags->pluck('id')->toArray();

// dd($tagIds);

$query->where('taggables.tag_id', '=', 1);
$query->whereIn('taggables.tag_id', $tagIds);
});
}

Expand Down

0 comments on commit c12ab3f

Please sign in to comment.