mirror of
https://github.com/snipe/snipe-it.git
synced 2025-02-21 03:15:45 -08:00
Merge pull request #14308 from snipe/fixes/eager_load_relations_on_locations
Eager load relations to determine deletability on locations
This commit is contained in:
commit
ffa7d25fc0
|
@ -289,7 +289,12 @@ class LocationsController extends Controller
|
||||||
|
|
||||||
// Make sure some IDs have been selected
|
// Make sure some IDs have been selected
|
||||||
if ((is_array($locations_raw_array)) && (count($locations_raw_array) > 0)) {
|
if ((is_array($locations_raw_array)) && (count($locations_raw_array) > 0)) {
|
||||||
$locations = Location::whereIn('id', $locations_raw_array)->get();
|
$locations = Location::whereIn('id', $locations_raw_array)
|
||||||
|
->withCount('assignedAssets as assigned_assets_count')
|
||||||
|
->withCount('assets as assets_count')
|
||||||
|
->withCount('rtd_assets as rtd_assets_count')
|
||||||
|
->withCount('children as children_count')
|
||||||
|
->withCount('users as users_count')->get();
|
||||||
|
|
||||||
$valid_count = 0;
|
$valid_count = 0;
|
||||||
foreach ($locations as $location) {
|
foreach ($locations as $location) {
|
||||||
|
|
|
@ -95,7 +95,10 @@ class Location extends SnipeModel
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Determine whether or not this location can be deleted
|
* Determine whether or not this location can be deleted.
|
||||||
|
*
|
||||||
|
* This method requires the eager loading of the relationships in order to determine whether
|
||||||
|
* it can be deleted. It's tempting to load those here, but that increases the query load considerably.
|
||||||
*
|
*
|
||||||
* @author A. Gianotto <snipe@snipe.net>
|
* @author A. Gianotto <snipe@snipe.net>
|
||||||
* @since [v3.0]
|
* @since [v3.0]
|
||||||
|
@ -104,10 +107,10 @@ class Location extends SnipeModel
|
||||||
public function isDeletable()
|
public function isDeletable()
|
||||||
{
|
{
|
||||||
return Gate::allows('delete', $this)
|
return Gate::allows('delete', $this)
|
||||||
&& ($this->assignedAssets()->count() === 0)
|
&& ($this->assets_count === 0)
|
||||||
&& ($this->assets()->count() === 0)
|
&& ($this->assigned_assets_count === 0)
|
||||||
&& ($this->children()->count() === 0)
|
&& ($this->children_count === 0)
|
||||||
&& ($this->users()->count() === 0);
|
&& ($this->users_count === 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Reference in a new issue