Location.php
Go to the documentation of this file.
1 <?php
2 namespace App\Models;
3 
9 
10 class Location extends Model
11 {
12  use SoftDeletes;
13  protected $dates = ['deleted_at'];
14  protected $table = 'locations';
15  protected $rules = array(
16  'name' => 'required|min:3|max:255|unique:locations,name,NULL,deleted_at',
17  'city' => 'min:3|max:255',
18  'state' => 'min:2|max:32',
19  'country' => 'min:2|max:2|max:2',
20  'address' => 'min:5|max:80',
21  'address2' => 'min:2|max:80',
22  'zip' => 'min:3|max:10',
23  );
24 
32  protected $injectUniqueIdentifier = true;
33  use ValidatingTrait;
34 
35 
41  protected $fillable = ['name'];
42 
43  public function users()
44  {
45  return $this->hasMany('\App\Models\User', 'location_id');
46  }
47 
48  public function assets()
49  {
50  return $this->hasManyThrough('\App\Models\Asset', '\App\Models\Actionlog', 'location_id', 'id');
51  }
52 
53  public function assignedassets()
54  {
55  return $this->hasMany('\App\Models\Asset', 'rtd_location_id');
56  }
57 
58  public function parent()
59  {
60  return $this->belongsTo('\App\Models\Location', 'parent_id');
61  }
62 
63  public function childLocations()
64  {
65  return $this->hasMany('\App\Models\Location', 'parent_id');
66  }
67 
68  public static function getLocationHierarchy($locations, $parent_id = null)
69  {
70 
71 
72  $op = array();
73 
74  foreach ($locations as $location) {
75 
76  if ($location['parent_id'] == $parent_id) {
77  $op[$location['id']] =
78  array(
79  'name' => $location['name'],
80  'parent_id' => $location['parent_id']
81  );
82 
83  // Using recursion
84  $children = Location::getLocationHierarchy($locations, $location['id']);
85  if ($children) {
86  $op[$location['id']]['children'] = $children;
87  }
88 
89  }
90 
91  }
92  return $op;
93  }
94 
95 
96  public static function flattenLocationsArray($location_options_array = null)
97  {
98  $location_options = array();
99  foreach ($location_options_array as $id => $value) {
100 
101  // get the top level key value
102  $location_options[$id] = $value['name'];
103 
104  // If there is a key named children, it has child locations and we have to walk it
105  if (array_key_exists('children', $value)) {
106 
107  foreach ($value['children'] as $child_id => $child_location_array) {
108  $child_location_options = Location::flattenLocationsArray($value['children']);
109 
110  foreach ($child_location_options as $child_id => $child_name) {
111  $location_options[$child_id] = '--'.$child_name;
112  }
113  }
114 
115  }
116 
117  }
118 
119  return $location_options;
120  }
121 
130  public function scopeTextsearch($query, $search)
131  {
132 
133  return $query->where('name', 'LIKE', "%$search%")
134  ->orWhere('address', 'LIKE', "%$search%")
135  ->orWhere('city', 'LIKE', "%$search%")
136  ->orWhere('state', 'LIKE', "%$search%")
137  ->orWhere('zip', 'LIKE', "%$search%")
138 
139  // This doesn't actually work - need to use a table alias maybe?
140  ->orWhere(function ($query) use ($search) {
141  $query->whereHas('parent', function ($query) use ($search) {
142  $query->where(function ($query) use ($search) {
143  $query->where('name', 'LIKE', '%'.$search.'%');
144  });
145  })
146  // Ugly, ugly code because Laravel sucks at self-joins
147  ->orWhere(function ($query) use ($search) {
148  $query->whereRaw("parent_id IN (select id from locations where name LIKE '%".$search."%') ");
149  });
150  });
151 
152  }
153 
154 
163  public function scopeOrderParent($query, $order)
164  {
165  // Left join here, or it will only return results with parents
166  return $query->leftJoin('locations as parent_loc', 'locations.parent_id', '=', 'parent_loc.id')->orderBy('parent_loc.name', $order);
167  }
168 }
scopeTextsearch($query, $search)
Query builder scope to search on text.
Definition: Location.php:130
static getLocationHierarchy($locations, $parent_id=null)
Definition: Location.php:68
static flattenLocationsArray($location_options_array=null)
Definition: Location.php:96
scopeOrderParent($query, $order)
Query builder scope to order on parent.
Definition: Location.php:163