2025-03-11 17:15:41 +05:30
|
|
|
<?php
|
|
|
|
|
|
2025-03-11 17:52:55 +05:30
|
|
|
namespace App\Http\Controllers\APIS\CustomerApi;
|
2025-03-11 17:15:41 +05:30
|
|
|
|
|
|
|
|
use App\Http\Controllers\Controller;
|
2025-03-11 17:52:55 +05:30
|
|
|
use App\Models\User;
|
2025-03-11 17:15:41 +05:30
|
|
|
use App\Models\UserAssetLink;
|
|
|
|
|
use Illuminate\Http\Request;
|
2025-03-11 19:44:37 +05:30
|
|
|
use Tymon\JWTAuth\Facades\JWTAuth;
|
2025-03-12 19:19:13 +05:30
|
|
|
use Illuminate\Container\Attributes\Auth;
|
|
|
|
|
use Illuminate\Database\QueryException;
|
|
|
|
|
use Illuminate\Support\Facades\Log;
|
2025-03-11 17:15:41 +05:30
|
|
|
class UserAssetLinkController extends Controller
|
|
|
|
|
{
|
2025-03-12 19:19:13 +05:30
|
|
|
|
2025-03-11 17:15:41 +05:30
|
|
|
public function index()
|
|
|
|
|
{
|
2025-03-11 19:44:37 +05:30
|
|
|
// $user = User::where('id', '8898f380-fd9e-11ef-a9dc-45dd276e4cd5')->first();
|
2025-03-11 19:05:25 +05:30
|
|
|
|
2025-03-18 12:25:57 +05:30
|
|
|
// $userAssetLinks = UserAssetLink::with(['user', 'asset.devices'])
|
|
|
|
|
// ->withCount([
|
|
|
|
|
// 'asset as active_devices_count' => function ($query) {
|
|
|
|
|
// $query->whereHas('devices', function ($q) {
|
|
|
|
|
// $q->where('active', 1);
|
|
|
|
|
// });
|
|
|
|
|
// },
|
|
|
|
|
// 'asset as inactive_devices_count' => function ($query) {
|
|
|
|
|
// $query->whereHas('devices', function ($q) {
|
|
|
|
|
// $q->where('active', 0);
|
|
|
|
|
// });
|
|
|
|
|
// }
|
|
|
|
|
// ])
|
|
|
|
|
// ->get();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$users = User::with([
|
|
|
|
|
'assets.devices',
|
|
|
|
|
])->withCount([
|
|
|
|
|
'assets as active_devices_count' => function ($query) {
|
2025-03-11 19:05:25 +05:30
|
|
|
$query->whereHas('devices', function ($q) {
|
|
|
|
|
$q->where('active', 1);
|
|
|
|
|
});
|
|
|
|
|
},
|
2025-03-18 12:25:57 +05:30
|
|
|
'assets as inactive_devices_count' => function ($query) {
|
2025-03-11 19:05:25 +05:30
|
|
|
$query->whereHas('devices', function ($q) {
|
|
|
|
|
$q->where('active', 0);
|
|
|
|
|
});
|
|
|
|
|
}
|
2025-03-18 12:25:57 +05:30
|
|
|
])->get();
|
2025-03-11 17:15:41 +05:30
|
|
|
|
2025-03-18 12:29:47 +05:30
|
|
|
|
|
|
|
|
|
2025-03-18 12:25:57 +05:30
|
|
|
return response()->json($users);
|
2025-03-11 17:15:41 +05:30
|
|
|
}
|
2025-03-11 19:05:25 +05:30
|
|
|
}
|