bearerToken(); // Or $request->header('Authorization') // $token = readHeaderToken(); // Or $request->header('Authorization') // dd($token['sub']); // // $user = Auth::user(); // // dd($user); // $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(); // return response()->json($userAssetLinks); // } public function index() { try { $token = readHeaderToken(); // dd($token['sub']); $user = User::where('id',$token['sub'])->first(); $userAssetLinks = UserAssetLink::with(['user', 'asset.devices']) ->where('user_id', $user->id) ->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(); return jsonResponseWithSuccessMessage(__('auth.data_fetched_successfully'), $userAssetLinks, 200); } catch (QueryException $e) { Log::error('Something went wrong: ' . $e->getMessage()); return jsonResponseWithErrorMessageApi(__('Something went wrong'), 500); } } }