sneha #13
@@ -12,62 +12,44 @@ use Illuminate\Database\QueryException;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
class UserAssetLinkController extends Controller
|
||||
{
|
||||
// public function index(Request $request)
|
||||
// {
|
||||
|
||||
// // $token = $request->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()
|
||||
{
|
||||
// $user = User::where('id', '8898f380-fd9e-11ef-a9dc-45dd276e4cd5')->first();
|
||||
|
||||
try {
|
||||
$token = readHeaderToken();
|
||||
// dd($token['sub']);
|
||||
$user = User::where('id',$token['sub'])->first();
|
||||
// $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();
|
||||
|
||||
$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);
|
||||
}
|
||||
$users = User::with([
|
||||
'assets.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);
|
||||
});
|
||||
}
|
||||
])->get();
|
||||
|
||||
|
||||
|
||||
return response()->json($users);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -63,7 +63,8 @@ class User extends Authenticatable implements JWTSubject
|
||||
}
|
||||
|
||||
public function assets()
|
||||
{
|
||||
return $this->hasManyThrough(Asset::class, UserAssetLink::class, 'user_id', 'id', 'id', 'asset_id');
|
||||
}
|
||||
}
|
||||
{
|
||||
return $this->hasManyThrough(Asset::class, UserAssetLink::class, 'user_id', 'id', 'id', 'asset_id');
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user