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-24 13:31:06 +05:30
|
|
|
use App\Models\Asset;
|
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-18 15:45:21 +05:30
|
|
|
|
2025-03-24 13:31:06 +05:30
|
|
|
// $token = readHeaderToken();
|
|
|
|
|
|
|
|
|
|
// $userAssetLinks = UserAssetLink::with(['user', 'asset.devices'])
|
|
|
|
|
// ->withCount([
|
|
|
|
|
// 'assets as active_devices_count' => function ($query) {
|
|
|
|
|
// $query->whereHas('devices', function ($q) {
|
|
|
|
|
// $q->where('active', 1);
|
|
|
|
|
// });
|
|
|
|
|
// },
|
|
|
|
|
// 'assets as inactive_devices_count' => function ($query) {
|
|
|
|
|
// $query->whereHas('devices', function ($q) {
|
|
|
|
|
// $q->where('active', 0);
|
|
|
|
|
// });
|
|
|
|
|
// }
|
|
|
|
|
// ])
|
|
|
|
|
// ->where('id', $token['sub'])
|
|
|
|
|
// ->first();
|
|
|
|
|
|
|
|
|
|
// return response()->json($userAssetLinks);
|
|
|
|
|
|
|
|
|
|
$token = readHeaderToken();
|
|
|
|
|
|
|
|
|
|
$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);
|
|
|
|
|
});
|
2025-03-11 17:15:41 +05:30
|
|
|
}
|
2025-03-24 13:31:06 +05:30
|
|
|
])
|
|
|
|
|
->get();
|
2025-03-18 15:45:21 +05:30
|
|
|
|
2025-03-24 13:31:06 +05:30
|
|
|
return response()->json($userAssetLinks)
|
|
|
|
|
->withHeaders([
|
|
|
|
|
'Authorization' => "Bearer $token"
|
|
|
|
|
]);
|
2025-03-18 15:45:21 +05:30
|
|
|
|
2025-03-11 19:05:25 +05:30
|
|
|
}
|
2025-03-24 13:31:06 +05:30
|
|
|
}
|