Skip to content

Instantly share code, notes, and snippets.

@valex
Created September 10, 2020 19:51
Show Gist options
  • Save valex/0c5c1f49a591d02ccf6da0606cb8cabf to your computer and use it in GitHub Desktop.
Save valex/0c5c1f49a591d02ccf6da0606cb8cabf to your computer and use it in GitHub Desktop.
<?php namespace App\Repositories;
use App\CarGroup;
use App\Manufacturer;
use App\Car;
use App\Tag;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Cache;
class TagRepository extends Repository {
public function getModels($manufacturerId, array $tags){
return Tag::where('manufacturer_id', $manufacturerId)
->whereIn('tag', $tags)
->get();
}
public function getCarsForTagIds($tag_ids){
$tag_ids = Arr::sort($tag_ids);
$tag_ids_imploded = implode ( '_' , $tag_ids );
$tag_ids_cache_key = md5($tag_ids_imploded);
return Cache::remember('cars_for_tag_ids_'.$tag_ids_cache_key, 259200, function () use($tag_ids) {
$car_ids = \DB::table('car_tag')->distinct()->whereIn('tag_id', $tag_ids)
->pluck('car_id');
return Car::whereIn('id', $car_ids)->get();
});
}
public function getTagsForCarIds($car_ids){
$car_ids = Arr::sort($car_ids);
$car_ids_imploded = implode ( '_' , $car_ids );
$car_ids_cache_key = md5($car_ids_imploded);
return Cache::remember('tags_for_car_ids_'.$car_ids_cache_key, 259200, function () use($car_ids) {
$tag_ids = \DB::table('car_tag')->distinct()->whereIn('car_id', $car_ids)
->pluck('tag_id');
return Tag::whereIn('id', $tag_ids)->orderBy('tag','asc')->get();
});
}
public function isAllTagsExist($manufacturerId, array $tags){
$collection = Tag::where('manufacturer_id', $manufacturerId)
->whereIn('tag', $tags)
->get();
return count($tags) === $collection->count();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment