sayali #101
@@ -136,21 +136,85 @@ class AssetadmintController extends Controller
|
||||
|
||||
|
||||
|
||||
// public function listAssest()
|
||||
// {
|
||||
// try {
|
||||
// $assets = Asset::with('customer')->get()->map(function ($asset) {
|
||||
// $assetData = $asset->toArray();
|
||||
// unset($assetData['customer']);
|
||||
// $assetData['customer_name'] = $asset->customer?->name;
|
||||
// return $assetData;
|
||||
// });
|
||||
|
||||
// return jsonResponseWithSuccessMessage('Assets fetched successfully', [
|
||||
// 'assests' => $assets
|
||||
// ]);
|
||||
// } catch (Exception $e) {
|
||||
|
||||
// Log::error("An error occurred: " . $e->getMessage());
|
||||
// return jsonResponseWithErrorMessage($e->getMessage(), 500);
|
||||
// }
|
||||
// }
|
||||
|
||||
// public function listAssest()
|
||||
// {
|
||||
// try {
|
||||
// $assets = Asset::with([
|
||||
// 'customer:id,name',
|
||||
// 'userAssetLinks.user:id,name' // eager load user names
|
||||
// ])->get()->map(function ($asset) {
|
||||
// $assetData = $asset->toArray();
|
||||
|
||||
// unset($assetData['customer'], $assetData['user_asset_links']); // remove full objects
|
||||
|
||||
// $assetData['customer_name'] = $asset->customer?->name;
|
||||
|
||||
// // Collect user names from user_asset_links
|
||||
// $userNames = $asset->userAssetLinks->map(function ($userAssetLink) {
|
||||
// return optional($userAssetLink->user)->name;
|
||||
// })->filter()->values(); // remove nulls and reset keys
|
||||
|
||||
// $assetData['user_names'] = $userNames;
|
||||
|
||||
// return $assetData;
|
||||
// });
|
||||
|
||||
// return jsonResponseWithSuccessMessage('Assets fetched successfully', [
|
||||
// 'assets' => $assets
|
||||
// ]);
|
||||
// } catch (Exception $e) {
|
||||
// Log::error("An error occurred: " . $e->getMessage());
|
||||
// return jsonResponseWithErrorMessage($e->getMessage(), 500);
|
||||
// }
|
||||
// }
|
||||
|
||||
public function listAssest()
|
||||
{
|
||||
try {
|
||||
$assets = Asset::with('customer')->get()->map(function ($asset) {
|
||||
$assets = Asset::with([
|
||||
'customer:id,name',
|
||||
'userAssetLinks.user:id,first_name' // eager load first_name only
|
||||
])->get()->map(function ($asset) {
|
||||
$assetData = $asset->toArray();
|
||||
unset($assetData['customer']);
|
||||
|
||||
unset($assetData['customer'], $assetData['user_asset_links']); // remove full objects
|
||||
|
||||
$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;
|
||||
})->filter()->values(); // remove nulls and reset keys
|
||||
|
||||
$assetData['assign_to_user'] = $userFirstNames;
|
||||
|
||||
return $assetData;
|
||||
});
|
||||
|
||||
return jsonResponseWithSuccessMessage('Assets fetched successfully', [
|
||||
'assests' => $assets
|
||||
'assets' => $assets
|
||||
]);
|
||||
} catch (Exception $e) {
|
||||
|
||||
Log::error("An error occurred: " . $e->getMessage());
|
||||
return jsonResponseWithErrorMessage($e->getMessage(), 500);
|
||||
}
|
||||
@@ -160,6 +224,8 @@ class AssetadmintController extends Controller
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public function deleteAsset(Request $request)
|
||||
{
|
||||
$assetId = $request->input('assetId'); // Fetching ID from request body
|
||||
@@ -328,17 +394,53 @@ class AssetadmintController extends Controller
|
||||
}
|
||||
|
||||
|
||||
// public function assestlistCustomer($customerId)
|
||||
// {
|
||||
// try {
|
||||
// $assets = Asset::with('customer:id,name')
|
||||
// ->where('customer_xid', $customerId)
|
||||
// ->get()
|
||||
// ->map(function ($asset) {
|
||||
// return collect($asset)
|
||||
// ->except('customer') // remove the full customer object
|
||||
// ->merge([
|
||||
// 'customer_name' => optional($asset->customer)->name,
|
||||
// ]);
|
||||
// });
|
||||
|
||||
// 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')
|
||||
$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') // remove the full customer object
|
||||
->except('customer', 'userAssetLinks') // remove full objects
|
||||
->merge([
|
||||
'customer_name' => optional($asset->customer)->name,
|
||||
'assign_to_user' => $userNames, // attach user names
|
||||
]);
|
||||
});
|
||||
|
||||
|
||||
@@ -57,6 +57,8 @@ class Asset extends Model
|
||||
{
|
||||
return $this->hasMany(Device::class, 'asset_id', 'id');
|
||||
}
|
||||
|
||||
|
||||
public function userAssetLinks()
|
||||
{
|
||||
return $this->hasMany(UserAssetLink::class, 'asset_id', 'id');
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user