Files
site/app/Domain/Feeds/Service/FeedService.php
2025-04-21 16:03:20 +02:00

61 lines
2.0 KiB
PHP

<?php
namespace App\Domain\Feeds\Service;
use App\Domain\Feeds\DataTransferObjects\FeedData;
use App\Domain\Feeds\Models\Feed;
use App\Domain\Images\Models\Image;
use App\Domain\Musics\Models\Music;
use App\Domain\Videos\Models\Video;
use Illuminate\Database\Eloquent\Relations\MorphTo;
use Illuminate\Database\Eloquent\Builder;
class FeedService
{
public static function list($users, $where = null, $only = null)
{
$users_id = $users->pluck('id')->toArray();
$feeds = Feed::query();
if ($where) {
$feeds = $feeds->where('id', '<', $where);
}
if($only){
if($only == 'image'){
//$feeds = $feeds->whereHasMorph('feedable', [Image::class]);
$feeds = $feeds->where('type', 'images');
}
if($only == 'video'){
//$feeds = $feeds->whereHasMorph('feedable', [Video::class]);
$feeds = $feeds->where('type', 'videos');
}
if($only == 'music'){
//$feeds = $feeds->whereHasMorph('feedable', [Music::class]);
$feeds = $feeds->where('type', 'musics');
}
}
$feeds = $feeds->whereIn('user_id', $users_id)
->orderBy('id', 'desc')
->limit(10)
->withCount([ 'comments', 'likes', 'likes as liked' => function (Builder $query) {
$query->where('user_id', auth()->user()->id);
}])
->withCasts(['liked' => 'boolean'])
->with('tags', 'media')->get();
$feed_collect = collect();
foreach ($feeds as $feed) {
$feed = FeedData::fromDataBase($feed, $users);
$feed['entity']->tags->transform(function ($item) {
return [
'id' => $item->id,
'name' => $item->name,
'slug' => $item->slug,
];
});
$feed_collect[] = $feed;
}
return $feed_collect;
}
}