sneha #71
@@ -84,7 +84,7 @@ class AssetadmintController extends Controller
|
||||
try {
|
||||
$assets = Asset::with('customer')->get()->map(function ($asset) {
|
||||
$assetData = $asset->toArray();
|
||||
unset($assetData['customer']);
|
||||
unset($assetData['customer']);
|
||||
$assetData['customer_name'] = $asset->customer?->name;
|
||||
return $assetData;
|
||||
});
|
||||
@@ -214,16 +214,32 @@ class AssetadmintController extends Controller
|
||||
|
||||
public function assestlistCustomer($customerId)
|
||||
{
|
||||
$assets = Asset::where('customer_xid', $customerId)->get();
|
||||
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'], 404);
|
||||
if ($assets->isEmpty()) {
|
||||
return response()->json(['message' => 'No assets found for this customer ID'], 404);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
return jsonResponseWithSuccessMessage('Assests fetched successfully', [
|
||||
'Assests' => $assets
|
||||
]);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -182,20 +182,33 @@ class DeviceController extends Controller
|
||||
public function devicelistCustomer($customerId)
|
||||
{
|
||||
try {
|
||||
$devices = Device::with('deviceProfile:id,name', 'customer:id,name')
|
||||
->where('customer_id', $customerId)
|
||||
->get()
|
||||
->map(function ($device) {
|
||||
$deviceArray = $device->toArray();
|
||||
|
||||
$devices = Device::where('customer_id', $customerId)->get();
|
||||
unset($deviceArray['device_profile'], $deviceArray['customer']);
|
||||
|
||||
$deviceArray['customer_name'] = optional($device->customer)->name;
|
||||
$deviceArray['device_profile_name'] = optional($device->deviceProfile)->name;
|
||||
|
||||
return $deviceArray;
|
||||
});
|
||||
|
||||
if ($devices->isEmpty()) {
|
||||
return response()->json(['message' => 'No devices found for this customer ID'], 404);
|
||||
}
|
||||
|
||||
return jsonResponseWithSuccessMessage('Devices fetched successfully', [
|
||||
'Devices' => $devices
|
||||
'devices' => $devices
|
||||
]);
|
||||
} catch (Exception $e) {
|
||||
Log::error("An error occurred in customer device listing: " . $e->getMessage());
|
||||
|
||||
return jsonResponseWithErrorMessage($e->getMessage(), 500);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -17,7 +17,7 @@ class Asset extends Model
|
||||
'entity_type',
|
||||
'created_time',
|
||||
'tenant_id',
|
||||
'customer_id',
|
||||
'customer_xid',
|
||||
'name',
|
||||
'type',
|
||||
'label',
|
||||
|
||||
@@ -52,9 +52,9 @@ class Device extends Model
|
||||
{
|
||||
return $this->hasMany(TimeseriesKeyMaster::class, 'device_profile_xid', 'device_profile_id');
|
||||
}
|
||||
|
||||
|
||||
public function customer()
|
||||
{
|
||||
return $this->belongsTo(Customer::class, 'customer_xid', 'id');
|
||||
return $this->belongsTo(Customer::class, 'customer_id', 'id');
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user