Skip to content

A free open source IT asset/license management system

Update withCounts because Laravel 5.5 :(

snipe0714ac42489b
diff --git a/app/Console/Commands/SyncAssetCounters.php b/app/Console/Commands/SyncAssetCounters.php
index bf63eaff9d..33fee840d6 100644
--- a/app/Console/Commands/SyncAssetCounters.php
+++ b/app/Console/Commands/SyncAssetCounters.php
@@ -39,7 +39,7 @@ class SyncAssetCounters extends Command
public function handle()
{
$start = microtime(true);
- $assets = Asset::withCount('checkins', 'checkouts', 'userRequests')
+ $assets = Asset::withCount('checkins as checkins_count', 'checkouts as checkouts_count', 'userRequests as user_requests_count')
->withTrashed()->get();
if ($assets) {
diff --git a/app/Helpers/Helper.php b/app/Helpers/Helper.php
index bd4b137590..b8ef8ccda6 100644
--- a/app/Helpers/Helper.php
+++ b/app/Helpers/Helper.php
@@ -281,9 +281,9 @@ class Helper
*/
public static function checkLowInventory()
{
- $consumables = Consumable::withCount('consumableAssignments')->whereNotNull('min_amt')->get();
- $accessories = Accessory::withCount('users')->whereNotNull('min_amt')->get();
- $components = Component::withCount('assets')->whereNotNull('min_amt')->get();
+ $consumables = Consumable::withCount('consumableAssignments as consumable_assignments_count')->whereNotNull('min_amt')->get();
+ $accessories = Accessory::withCount('users as users_count')->whereNotNull('min_amt')->get();
+ $components = Component::withCount('assets as assets_count')->whereNotNull('min_amt')->get();
$avail_consumables = 0;
$items_array = array();
diff --git a/app/Http/Controllers/Api/AssetModelsController.php b/app/Http/Controllers/Api/AssetModelsController.php
index 1d9d9c4d4a..7e28949d2c 100644
--- a/app/Http/Controllers/Api/AssetModelsController.php
+++ b/app/Http/Controllers/Api/AssetModelsController.php
@@ -48,7 +48,7 @@ class AssetModelsController extends Controller
'models.updated_at',
])
->with('category','depreciation', 'manufacturer','fieldset')
- ->withCount('assets');
+ ->withCount('assets as assets_count');
@@ -114,7 +114,7 @@ class AssetModelsController extends Controller
public function show($id)
{
$this->authorize('view', AssetModel::class);
- $assetmodel = AssetModel::withCount('assets')->findOrFail($id);
+ $assetmodel = AssetModel::withCount('assets as assets_count')->findOrFail($id);
return (new AssetModelsTransformer)->transformAssetModel($assetmodel);
}
diff --git a/app/Http/Controllers/Api/AssetsController.php b/app/Http/Controllers/Api/AssetsController.php
index 63e7ac0f37..21ad2c6453 100644
--- a/app/Http/Controllers/Api/AssetsController.php
+++ b/app/Http/Controllers/Api/AssetsController.php
@@ -325,7 +325,8 @@ class AssetsController extends Controller
*/
public function show($id)
{
- if ($asset = Asset::with('assetstatus')->with('assignedTo')->withTrashed()->withCount('checkins', 'checkouts', 'userRequests')->findOrFail($id)) {
+ if ($asset = Asset::with('assetstatus')->with('assignedTo')->withTrashed()
+ ->withCount('checkins as checkins_count', 'checkouts as checkouts_count', 'userRequests as user_requests_count')->findOrFail($id)) {
$this->authorize('view', $asset);
return (new AssetsTransformer)->transformAsset($asset);
}
diff --git a/app/Http/Controllers/Api/CategoriesController.php b/app/Http/Controllers/Api/CategoriesController.php
index 190b6f248a..86e826de77 100644
--- a/app/Http/Controllers/Api/CategoriesController.php
+++ b/app/Http/Controllers/Api/CategoriesController.php
@@ -24,7 +24,7 @@ class CategoriesController extends Controller
$allowed_columns = ['id', 'name','category_type', 'category_type','use_default_eula','eula_text', 'require_acceptance','checkin_email', 'assets_count', 'accessories_count', 'consumables_count', 'components_count','licenses_count', 'image'];
$categories = Category::select(['id', 'created_at', 'updated_at', 'name','category_type','use_default_eula','eula_text', 'require_acceptance','checkin_email','image'])
- ->withCount('assets', 'accessories', 'consumables', 'components','licenses');
+ ->withCount('assets as assets_count', 'accessories as accessories_count', 'consumables as consumables_count', 'components as components_count','licenses as licenses_count');
if ($request->has('search')) {
$categories = $categories->TextSearch($request->input('search'));
diff --git a/app/Http/Controllers/Api/CompaniesController.php b/app/Http/Controllers/Api/CompaniesController.php
index c960561b48..8ad97ed2d6 100644
--- a/app/Http/Controllers/Api/CompaniesController.php
+++ b/app/Http/Controllers/Api/CompaniesController.php
@@ -35,7 +35,7 @@ class CompaniesController extends Controller
'components_count',
];
- $companies = Company::withCount('assets','licenses','accessories','consumables','components','users');
+ $companies = Company::withCount('assets as assets_count','licenses as licenses_count','accessories as accessories_count','consumables as consumables_count','components as components_count','users as users_count');
if ($request->has('search')) {
$companies->TextSearch($request->input('search'));
diff --git a/app/Http/Controllers/Api/CustomFieldsetsController.php b/app/Http/Controllers/Api/CustomFieldsetsController.php
index 3946be433c..f5cfafdf93 100644
--- a/app/Http/Controllers/Api/CustomFieldsetsController.php
+++ b/app/Http/Controllers/Api/CustomFieldsetsController.php
@@ -43,7 +43,7 @@ class CustomFieldsetsController extends Controller
public function index()
{
$this->authorize('index', CustomFieldset::class);
- $fieldsets = CustomFieldset::withCount(['fields', 'models'])->get();
+ $fieldsets = CustomFieldset::withCount('fields as fields_count', 'models as models_count')->get();
return (new CustomFieldsetsTransformer)->transformCustomFieldsets($fieldsets, $fieldsets->count());
}
diff --git a/app/Http/Controllers/Api/DepartmentsController.php b/app/Http/Controllers/Api/DepartmentsController.php
index ab2132707d..1f2437dc19 100644
--- a/app/Http/Controllers/Api/DepartmentsController.php
+++ b/app/Http/Controllers/Api/DepartmentsController.php
@@ -33,7 +33,7 @@ class DepartmentsController extends Controller
'departments.created_at',
'departments.updated_at',
'departments.image'
- ])->with('users')->with('location')->with('manager')->with('company')->withCount('users');
+ ])->with('users')->with('location')->with('manager')->with('company')->withCount('users as users_count');
if ($request->has('search')) {
$departments = $departments->TextSearch($request->input('search'));
diff --git a/app/Http/Controllers/Api/GroupsController.php b/app/Http/Controllers/Api/GroupsController.php
index e4839b27e4..e125658560 100644
--- a/app/Http/Controllers/Api/GroupsController.php
+++ b/app/Http/Controllers/Api/GroupsController.php
@@ -22,7 +22,7 @@ class GroupsController extends Controller
$this->authorize('view', Group::class);
$allowed_columns = ['id','name','created_at', 'users_count'];
- $groups = Group::select('id','name','permissions','created_at','updated_at')->withCount('users');
+ $groups = Group::select('id','name','permissions','created_at','updated_at')->withCount('users as users_count');
if ($request->has('search')) {
$groups = $groups->TextSearch($request->input('search'));
diff --git a/app/Http/Controllers/Api/LicensesController.php b/app/Http/Controllers/Api/LicensesController.php
index af68208684..88c8b93769 100644
--- a/app/Http/Controllers/Api/LicensesController.php
+++ b/app/Http/Controllers/Api/LicensesController.php
@@ -25,7 +25,7 @@ class LicensesController extends Controller
public function index(Request $request)
{
$this->authorize('view', License::class);
- $licenses = Company::scopeCompanyables(License::with('company', 'manufacturer', 'freeSeats', 'supplier','category')->withCount('freeSeats'));
+ $licenses = Company::scopeCompanyables(License::with('company', 'manufacturer', 'freeSeats', 'supplier','category')->withCount('freeSeats as free_seats_count'));
if ($request->has('company_id')) {
diff --git a/app/Http/Controllers/Api/LocationsController.php b/app/Http/Controllers/Api/LocationsController.php
index 3f77efab83..9dd478637d 100644
--- a/app/Http/Controllers/Api/LocationsController.php
+++ b/app/Http/Controllers/Api/LocationsController.php
@@ -41,9 +41,9 @@ class LocationsController extends Controller
'locations.updated_at',
'locations.image',
'locations.currency'
- ])->withCount('assignedAssets')
- ->withCount('assets')
- ->withCount('users');
+ ])->withCount('assignedAssets as assigned_assets_count')
+ ->withCount('assets as assets_count')
+ ->withCount('users as users_count');
if ($request->has('search')) {
$locations = $locations->TextSearch($request->input('search'));
diff --git a/app/Http/Controllers/Api/ManufacturersController.php b/app/Http/Controllers/Api/ManufacturersController.php
index 88d1d3a321..e0f48413f6 100644
--- a/app/Http/Controllers/Api/ManufacturersController.php
+++ b/app/Http/Controllers/Api/ManufacturersController.php
@@ -26,7 +26,7 @@ class ManufacturersController extends Controller
$manufacturers = Manufacturer::select(
array('id','name','url','support_url','support_email','support_phone','created_at','updated_at','image', 'deleted_at')
- )->withCount('assets')->withCount('licenses')->withCount('consumables')->withCount('accessories');
+ )->withCount('assets as assets_count')->withCount('licenses as licenses_count')->withCount('consumables as consumables_count')->withCount('accessories as accessories_count');
if ($request->input('deleted')=='true') {
$manufacturers->onlyTrashed();
diff --git a/app/Http/Controllers/Api/StatuslabelsController.php b/app/Http/Controllers/Api/StatuslabelsController.php
index ee2a486e70..8467e19f04 100644
--- a/app/Http/Controllers/Api/StatuslabelsController.php
+++ b/app/Http/Controllers/Api/StatuslabelsController.php
@@ -24,7 +24,7 @@ class StatuslabelsController extends Controller
$this->authorize('view', Statuslabel::class);
$allowed_columns = ['id','name','created_at', 'assets_count','color','default_label'];
- $statuslabels = Statuslabel::withCount('assets');
+ $statuslabels = Statuslabel::withCount('assets as assets_count');
if ($request->has('search')) {
$statuslabels = $statuslabels->TextSearch($request->input('search'));
@@ -162,7 +162,7 @@ class StatuslabelsController extends Controller
{
$this->authorize('view', Statuslabel::class);
- $statuslabels = Statuslabel::with('assets')->groupBy('id')->withCount('assets')->get();
+ $statuslabels = Statuslabel::with('assets')->groupBy('id')->withCount('assets as assets_count')->get();
$labels=[];
$points=[];
diff --git a/app/Http/Controllers/Api/SuppliersController.php b/app/Http/Controllers/Api/SuppliersController.php
index 27cee15216..0fe5ffa588 100644
--- a/app/Http/Controllers/Api/SuppliersController.php
+++ b/app/Http/Controllers/Api/SuppliersController.php
@@ -26,7 +26,7 @@ class SuppliersController extends Controller
$suppliers = Supplier::select(
array('id','name','address','address2','city','state','country','fax', 'phone','email','contact','created_at','updated_at','deleted_at','image','notes')
- )->withCount('assets')->withCount('licenses')->withCount('accessories');
+ )->withCount('assets as assets_count')->withCount('licenses as licenses_count')->withCount('accessories as accessories_count');
if ($request->has('search')) {
@@ -115,7 +115,7 @@ class SuppliersController extends Controller
public function destroy($id)
{
$this->authorize('delete', Supplier::class);
- $supplier = Supplier::with('asset_maintenances', 'assets', 'licenses')->withCount('asset_maintenances','assets', 'licenses')->findOrFail($id);
+ $supplier = Supplier::with('asset_maintenances', 'assets', 'licenses')->withCount('asset_maintenances as asset_maintenances_count','assets as assets_count', 'licenses as licenses_count')->findOrFail($id);
$this->authorize('delete', $supplier);
diff --git a/app/Http/Controllers/Api/UsersController.php b/app/Http/Controllers/Api/UsersController.php
index 02fcb77840..90514eb29b 100644
--- a/app/Http/Controllers/Api/UsersController.php
+++ b/app/Http/Controllers/Api/UsersController.php
@@ -56,7 +56,7 @@ class UsersController extends Controller
'users.zip',
])->with('manager', 'groups', 'userloc', 'company', 'department','assets','licenses','accessories','consumables')
- ->withCount('assets','licenses','accessories','consumables');
+ ->withCount('assets as assets_count','licenses as licneses_count','accessories as accessories_count','consumables as consumables_count');
$users = Company::scopeCompanyables($users);
diff --git a/app/Http/Controllers/BulkAssetModelsController.php b/app/Http/Controllers/BulkAssetModelsController.php
index 2d1e949653..52ccc25266 100644
--- a/app/Http/Controllers/BulkAssetModelsController.php
+++ b/app/Http/Controllers/BulkAssetModelsController.php
@@ -26,7 +26,7 @@ class BulkAssetModelsController extends Controller
if ((is_array($models_raw_array)) && (count($models_raw_array) > 0)) {
$models = AssetModel::whereIn('id', $models_raw_array)
- ->withCount('assets')
+ ->withCount('assets as assets_count')
->orderBy('assets_count', 'ASC')
->get();
@@ -107,7 +107,7 @@ class BulkAssetModelsController extends Controller
if ((is_array($models_raw_array)) && (count($models_raw_array) > 0)) {
- $models = AssetModel::whereIn('id', $models_raw_array)->withCount('assets')->get();
+ $models = AssetModel::whereIn('id', $models_raw_array)->withCount('assets as assets_count')->get();
$del_error_count = 0;
$del_count = 0;
diff --git a/app/Http/Controllers/SuppliersController.php b/app/Http/Controllers/SuppliersController.php
index 434b72a9c6..5af0097ee3 100755
--- a/app/Http/Controllers/SuppliersController.php
+++ b/app/Http/Controllers/SuppliersController.php
@@ -154,7 +154,7 @@ class SuppliersController extends Controller
public function destroy($supplierId)
{
$this->authorize('delete', Supplier::class);
- if (is_null($supplier = Supplier::with('asset_maintenances', 'assets', 'licenses')->withCount('asset_maintenances','assets','licenses')->find($supplierId))) {
+ if (is_null($supplier = Supplier::with('asset_maintenances', 'assets', 'licenses')->withCount('asset_maintenances as asset_maintenances_count','assets as assets_count','licenses as licenses_count')->find($supplierId))) {
return redirect()->route('suppliers.index')->with('error', trans('admin/suppliers/message.not_found'));
}
diff --git a/tests/api/ApiCategoriesCest.php b/tests/api/ApiCategoriesCest.php
index b97b29e1d9..993521706a 100644
--- a/tests/api/ApiCategoriesCest.php
+++ b/tests/api/ApiCategoriesCest.php
@@ -31,7 +31,7 @@ class ApiCategoriesCest
$response = json_decode($I->grabResponse(), true);
// sample verify
- $category = App\Models\Category::withCount('assets','accessories','consumables','components','licenses')
+ $category = App\Models\Category::withCount('assets as assets_count','accessories as accessories_count','consumables as consumables_count','components as components_count','licenses as licenses_count')
->orderByDesc('id')->take(10)->get()->shuffle()->first();
$I->seeResponseContainsJson($I->removeTimestamps((new CategoriesTransformer)->transformCategory($category)));
}
diff --git a/tests/api/ApiCompaniesCest.php b/tests/api/ApiCompaniesCest.php
index 9ae03faea0..a6ad1345e7 100644
--- a/tests/api/ApiCompaniesCest.php
+++ b/tests/api/ApiCompaniesCest.php
@@ -32,7 +32,7 @@ class ApiCompaniesCest
$response = json_decode($I->grabResponse(), true);
// dd($response);
// sample verify
- $company = App\Models\Company::withCount('assets','licenses','accessories','consumables','components','users')
+ $company = App\Models\Company::withCount('assets as assets_count','licenses as licenses_count','accessories as accessories_count','consumables as consumables_count','components as components_count','users as users_count')
->orderByDesc('created_at')->take(10)->get()->shuffle()->first();
$I->seeResponseContainsJson($I->removeTimestamps((new CompaniesTransformer)->transformCompany($company)));
diff --git a/tests/api/ApiLocationsCest.php b/tests/api/ApiLocationsCest.php
index 064488e33e..76573a35b7 100644
--- a/tests/api/ApiLocationsCest.php
+++ b/tests/api/ApiLocationsCest.php
@@ -32,7 +32,7 @@ class ApiLocationsCest
$response = json_decode($I->grabResponse(), true);
// sample verify
$location = App\Models\Location::orderByDesc('created_at')
- ->withCount('assignedAssets', 'assets', 'users')
+ ->withCount('assignedAssets as assigned_assets_count', 'assets as assets_count', 'users as users_count')
->take(10)->get()->shuffle()->first();
$I->seeResponseContainsJson($I->removeTimestamps((new LocationsTransformer)->transformLocation($location)));
}
diff --git a/tests/api/ApiModelsCest.php b/tests/api/ApiModelsCest.php
index 7df79eb0fb..ed068bfeab 100644
--- a/tests/api/ApiModelsCest.php
+++ b/tests/api/ApiModelsCest.php
@@ -31,7 +31,7 @@ class ApiAssetModelsCest
$response = json_decode($I->grabResponse(), true);
$assetmodel = App\Models\AssetModel::orderByDesc('created_at')
- ->withCount('assets')->take(10)->get()->shuffle()->first();
+ ->withCount('assets as assets_count')->take(10)->get()->shuffle()->first();
$I->seeResponseContainsJson($I->removeTimestamps((new AssetModelsTransformer)->transformAssetModel($assetmodel)));
}
diff --git a/tests/api/ApiStatusLabelsCest.php b/tests/api/ApiStatusLabelsCest.php
index 49aac413ae..2a3c7c26ce 100644
--- a/tests/api/ApiStatusLabelsCest.php
+++ b/tests/api/ApiStatusLabelsCest.php
@@ -32,7 +32,7 @@ class ApiStatuslabelsCest
$response = json_decode($I->grabResponse(), true);
// sample verify
$statuslabel = App\Models\Statuslabel::orderByDesc('created_at')
- ->withCount('assets')
+ ->withCount('assets as assets_count')
->take(10)->get()->shuffle()->first();
$I->seeResponseContainsJson($I->removeTimestamps((new StatuslabelsTransformer)->transformStatuslabel($statuslabel)));
}
diff --git a/tests/api/ApiUsersCest.php b/tests/api/ApiUsersCest.php
index b1063acf06..8d8d7629df 100644
--- a/tests/api/ApiUsersCest.php
+++ b/tests/api/ApiUsersCest.php
@@ -31,7 +31,7 @@ class ApiUsersCest
$response = json_decode($I->grabResponse(), true);
// sample verify
$user = App\Models\User::orderByDesc('created_at')
- ->withCount('assets', 'licenses', 'accessories', 'consumables')
+ ->withCount('assets as assets_count', 'licenses as licenses_count', 'accessories as accessories_count', 'consumables as consumables_count')
->take(10)->get()->shuffle()->first();
$I->seeResponseContainsJson($I->removeTimestamps((new UsersTransformer)->transformUser($user)));
}