FollowingController.php
1.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
<?php
namespace App\Http\Container\AppSection\Controllers\User;
use App\Helpers\JsonResource;
use App\Models\User;
use App\Models\UserFollowRelation;
use App\Support\Controller;
use Auth;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
class FollowingController extends Controller
{
/**
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\JsonResponse
*/
public function __invoke(Request $request, int $id): JsonResponse
{
$size = $request->get('size', 10);
$followingQuery = UserFollowRelation::query()->selectRaw('following_id,is_read,created_at as follower_at')->where('follower_id', $id)->groupBy('following_id');
$data = User::with(['userTag:id,name,icon,frame', 'authTags:id,name'])
->when($request->get('text'), fn(Builder $query, string $name) => $query->where('nick_name', 'like', '%' . $name . '%'))
->select(['id', 'nick_name', 'avatar', 'identity', 'user_tag_id', 'follower_at', 'is_read'])
->addSelect([
'is_follow' => UserFollowRelation::query()->selectRaw('IFNULL(COUNT(*),0)')->whereColumn('following_id', 'users.id')->where('follower_id', Auth::id()),
'is_following' => UserFollowRelation::query()->selectRaw('IFNULL(COUNT(*),0)')->whereColumn('follower_id', 'users.id')->where('following_id', Auth::id())
])
->joinSub($followingQuery, 'following', 'following.following_id', 'users.id')
->latest('follower_at')
->paginate($size);
return JsonResource::success(JsonResource::SUCCESS, ['data' => $data->items(), 'count' => $data->total()]);
}
}