username #102
@@ -161,7 +161,7 @@ class AssetadmintController extends Controller
|
||||
// try {
|
||||
// $assets = Asset::with([
|
||||
// 'customer:id,name',
|
||||
// 'userAssetLinks.user:id,name' // eager load user names
|
||||
// 'userAssetLinks.user:id,first_name' // eager load first_name only
|
||||
// ])->get()->map(function ($asset) {
|
||||
// $assetData = $asset->toArray();
|
||||
|
||||
@@ -169,12 +169,12 @@ class AssetadmintController extends Controller
|
||||
|
||||
// $assetData['customer_name'] = $asset->customer?->name;
|
||||
|
||||
// // Collect user names from user_asset_links
|
||||
// $userNames = $asset->userAssetLinks->map(function ($userAssetLink) {
|
||||
// return optional($userAssetLink->user)->name;
|
||||
// // Collect user first names from user_asset_links
|
||||
// $userFirstNames = $asset->userAssetLinks->map(function ($userAssetLink) {
|
||||
// return optional($userAssetLink->user)->first_name;
|
||||
// })->filter()->values(); // remove nulls and reset keys
|
||||
|
||||
// $assetData['user_names'] = $userNames;
|
||||
// $assetData['assign_to_user'] = $userFirstNames;
|
||||
|
||||
// return $assetData;
|
||||
// });
|
||||
@@ -193,7 +193,7 @@ class AssetadmintController extends Controller
|
||||
try {
|
||||
$assets = Asset::with([
|
||||
'customer:id,name',
|
||||
'userAssetLinks.user:id,first_name' // eager load first_name only
|
||||
'userAssetLinks.user:id,first_name,last_name' // eager load first_name and last_name
|
||||
])->get()->map(function ($asset) {
|
||||
$assetData = $asset->toArray();
|
||||
|
||||
@@ -201,12 +201,19 @@ class AssetadmintController extends Controller
|
||||
|
||||
$assetData['customer_name'] = $asset->customer?->name;
|
||||
|
||||
// Collect user first names from user_asset_links
|
||||
$userFirstNames = $asset->userAssetLinks->map(function ($userAssetLink) {
|
||||
return optional($userAssetLink->user)->first_name;
|
||||
// Collect user id and full name (first_name + last_name) from user_asset_links
|
||||
$assignToUsers = $asset->userAssetLinks->map(function ($userAssetLink) {
|
||||
$user = $userAssetLink->user;
|
||||
if ($user) {
|
||||
return [
|
||||
'user_id' => $user->id,
|
||||
'full_name' => trim($user->first_name . ' ' . $user->last_name),
|
||||
];
|
||||
}
|
||||
return null;
|
||||
})->filter()->values(); // remove nulls and reset keys
|
||||
|
||||
$assetData['assign_to_user'] = $userFirstNames;
|
||||
$assetData['assign_to_users'] = $assignToUsers;
|
||||
|
||||
return $assetData;
|
||||
});
|
||||
@@ -226,6 +233,7 @@ class AssetadmintController extends Controller
|
||||
|
||||
|
||||
|
||||
|
||||
public function deleteAsset(Request $request)
|
||||
{
|
||||
$assetId = $request->input('assetId'); // Fetching ID from request body
|
||||
@@ -421,26 +429,69 @@ class AssetadmintController extends Controller
|
||||
// }
|
||||
// }
|
||||
|
||||
// public function assestlistCustomer($customerId)
|
||||
// {
|
||||
// try {
|
||||
// $assets = Asset::with([
|
||||
// 'customer:id,name',
|
||||
// 'userAssetLinks.user:id,name' // eager load user through userAssetLink
|
||||
// ])
|
||||
// ->where('customer_xid', $customerId)
|
||||
// ->get()
|
||||
// ->map(function ($asset) {
|
||||
// // Get user names linked to the asset
|
||||
// $userNames = $asset->userAssetLinks->map(function ($userAssetLink) {
|
||||
// return optional($userAssetLink->user)->name;
|
||||
// })->filter()->values(); // filter nulls and reset keys
|
||||
|
||||
// return collect($asset)
|
||||
// ->except('customer', 'userAssetLinks') // remove full objects
|
||||
// ->merge([
|
||||
// 'customer_name' => optional($asset->customer)->name,
|
||||
// 'assign_to_user' => $userNames, // attach user names
|
||||
// ]);
|
||||
// });
|
||||
|
||||
// if ($assets->isEmpty()) {
|
||||
// return response()->json(['message' => 'No assets found for this customer ID'], 200);
|
||||
// }
|
||||
|
||||
// return jsonResponseWithSuccessMessage('Assets fetched successfully', [
|
||||
// 'assets' => $assets
|
||||
// ]);
|
||||
// } catch (Exception $e) {
|
||||
// Log::error("An error occurred in asset listing: " . $e->getMessage());
|
||||
// return jsonResponseWithErrorMessage($e->getMessage(), 500);
|
||||
// }
|
||||
// }
|
||||
|
||||
public function assestlistCustomer($customerId)
|
||||
{
|
||||
try {
|
||||
$assets = Asset::with([
|
||||
'customer:id,name',
|
||||
'userAssetLinks.user:id,name' // eager load user through userAssetLink
|
||||
'userAssetLinks.user:id,first_name,last_name' // eager load first_name and last_name
|
||||
])
|
||||
->where('customer_xid', $customerId)
|
||||
->get()
|
||||
->map(function ($asset) {
|
||||
// Get user names linked to the asset
|
||||
$userNames = $asset->userAssetLinks->map(function ($userAssetLink) {
|
||||
return optional($userAssetLink->user)->name;
|
||||
// Get user details (id and full name) linked to the asset
|
||||
$assignToUsers = $asset->userAssetLinks->map(function ($userAssetLink) {
|
||||
$user = $userAssetLink->user;
|
||||
if ($user) {
|
||||
return [
|
||||
'user_id' => $user->id,
|
||||
'full_name' => trim($user->first_name . ' ' . $user->last_name),
|
||||
];
|
||||
}
|
||||
return null;
|
||||
})->filter()->values(); // filter nulls and reset keys
|
||||
|
||||
return collect($asset)
|
||||
->except('customer', 'userAssetLinks') // remove full objects
|
||||
->merge([
|
||||
'customer_name' => optional($asset->customer)->name,
|
||||
'assign_to_user' => $userNames, // attach user names
|
||||
'assign_to_users' => $assignToUsers, // attach user_id and full_name
|
||||
]);
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user