и планшет user_post_intercrections < /code>, где я хранят (пользователь asper_id, post_id, inventuref_type). Теги, категория.
Как я могу сделать это без переполнения памяти (только с помощью SQL)? < /p>
Код: Выделить всё
$interactedPostIds = UserPostInteractions::where('user_id', Auth::user()->id)
->pluck('post_id');
$page = FacadesRequest::get('page', 1);
$perPage = 10;
//Get hashtags from those posts
$interactedHashtags = Posts::whereIn('id', $interactedPostIds)
->pluck('tags')
->map(function ($tags) {
return json_decode($tags); // Convert JSON string to array
})
->flatten()->countBy()->sortDesc()->keys()->toArray();
$userId = Auth::user()->id;
$posts = Posts::where('is_approved', 1)
->leftJoin('user_post_interactions', function ($join) use ($userId) {
$join->on('posts.id', '=', 'user_post_interactions.post_id')
->where('user_post_interactions.user_id', '=', $userId);
})
->selectRaw('posts.*, GROUP_CONCAT(DISTINCT user_post_interactions.interaction_type) as interaction_types')
->groupBy('posts.id') // Ensure unique posts
->get();
// Sort using tags match
$sorted = $posts->sortByDesc(function ($post) use ($interactedHashtags) {
$postTags = json_decode($post->tags, true) ?? [];
return count(array_intersect($postTags, $interactedHashtags));
});
$posts = new LengthAwarePaginator(
$sorted->forPage($page, $perPage),
$sorted->count(),
$perPage,
$page,
['path' => FacadesRequest::url(), 'query' => FacadesRequest::query()]
);
Подробнее здесь: https://stackoverflow.com/questions/796 ... uggestions
Мобильная версия