Fixed transformer

Signed-off-by: snipe <snipe@snipe.net>
This commit is contained in:
snipe 2024-08-14 19:33:23 +01:00
parent 0c2bed605f
commit 56b02df7aa

View file

@ -3,6 +3,7 @@
namespace App\Http\Transformers;
use App\Helpers\Helper;
use App\Models\Accessory;
use App\Models\Location;
use Illuminate\Support\Facades\Gate;
use Illuminate\Database\Eloquent\Collection;
@ -45,9 +46,9 @@ class LocationsTransformer
'zip' => ($location->zip) ? e($location->zip) : null,
'phone' => ($location->phone!='') ? e($location->phone): null,
'fax' => ($location->fax!='') ? e($location->fax): null,
'assigned_assets_count' => (int) $location->assigned_assets_count,
'accessories_count' => (int) $location->assigned_accessories,
'accessories_count' => (int) $location->accessories_count,
'assigned_accessories_count' => (int) $location->assigned_accessories_count,
'assigned_assets_count' => (int) $location->assigned_assets_count,
'assets_count' => (int) $location->assets_count,
'rtd_assets_count' => (int) $location->rtd_assets_count,
'users_count' => (int) $location->users_count,
@ -79,6 +80,37 @@ class LocationsTransformer
}
}
public function transformCheckedoutAccessories($accessory_checkouts, $total)
{
$array = [];
foreach ($accessory_checkouts as $checkout) {
$array = [
'id' => $checkout->id,
'accessory' => [
'id' => $checkout->accessory->id,
'name' => $checkout->accessory->name,
],
'image' => ($checkout->accessory->image) ? Storage::disk('public')->url('accessories/'.e($checkout->accessory->image)) : null,
'note' => $checkout->note ? e($checkout->note) : null,
'created_by' => $checkout->admin ? [
'id' => (int) $checkout->admin->id,
'name'=> e($checkout->admin->present()->fullName),
]: null,
'created_at' => Helper::getFormattedDateObject($checkout->created_at, 'datetime'),
];
$permissions_array['available_actions'] = [
'checkout' => Gate::allows('checkout', Accessory::class),
'checkin' => Gate::allows('checkin', Accessory::class),
];
$array += $permissions_array;
}
return (new DatatablesTransformer)->transformDatatables($array, $total);
}
/**
* This gives a compact view of the location data without any additional relational queries,
* allowing us to 1) deliver a smaller payload and 2) avoid additional queries on relations that