2023-11-16 13:06:22 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Services;
|
|
|
|
|
|
|
|
use DB;
|
|
|
|
use App\StatusHashtag;
|
|
|
|
use App\Models\HashtagRelated;
|
|
|
|
|
|
|
|
class HashtagRelatedService
|
|
|
|
{
|
|
|
|
public static function get($id)
|
|
|
|
{
|
2023-11-18 05:21:55 +00:00
|
|
|
$tag = HashtagRelated::whereHashtagId($id)->first();
|
|
|
|
if(!$tag) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
return $tag->related_tags;
|
2023-11-16 13:06:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public static function fetchRelatedTags($tag)
|
|
|
|
{
|
|
|
|
$res = StatusHashtag::query()
|
|
|
|
->select('h2.name', DB::raw('COUNT(*) as related_count'))
|
|
|
|
->join('status_hashtags as hs2', function ($join) {
|
|
|
|
$join->on('status_hashtags.status_id', '=', 'hs2.status_id')
|
2023-11-18 05:21:55 +00:00
|
|
|
->whereRaw('status_hashtags.hashtag_id != hs2.hashtag_id');
|
2023-11-16 13:06:22 +00:00
|
|
|
})
|
|
|
|
->join('hashtags as h1', 'status_hashtags.hashtag_id', '=', 'h1.id')
|
|
|
|
->join('hashtags as h2', 'hs2.hashtag_id', '=', 'h2.id')
|
|
|
|
->where('h1.name', '=', $tag)
|
|
|
|
->groupBy('h2.name')
|
|
|
|
->orderBy('related_count', 'desc')
|
2023-11-18 05:21:55 +00:00
|
|
|
->limit(30)
|
2023-11-16 13:06:22 +00:00
|
|
|
->get();
|
|
|
|
|
|
|
|
return $res;
|
|
|
|
}
|
|
|
|
}
|