Component.php
Go to the documentation of this file.
1 <?php
2 namespace App\Models;
3 
13 
19 class Component extends Model
20 {
21  use SoftDeletes;
22  use CompanyableTrait;
23 
24  protected $dates = ['deleted_at'];
25  protected $table = 'components';
26 
27 
31  public $rules = array(
32  'name' => 'required|min:3|max:255',
33  'min_amt' => 'integer|min:1',
34  );
35 
43  protected $injectUniqueIdentifier = true;
44  use ValidatingTrait;
45 
51  protected $fillable = ['name','company_id','category_id'];
52 
53  public function location()
54  {
55  return $this->belongsTo('\App\Models\Location', 'location_id');
56  }
57 
58  public function assets()
59  {
60  return $this->belongsToMany('\App\Models\Asset', 'components_assets')->withPivot('assigned_qty', 'created_at', 'user_id');
61  }
62 
63  public function admin()
64  {
65  return $this->belongsTo('\App\Models\User', 'user_id');
66  }
67 
68  public function company()
69  {
70  return $this->belongsTo('\App\Models\Company', 'company_id');
71  }
72 
73 
74  public function category()
75  {
76  return $this->belongsTo('\App\Models\Category', 'category_id');
77  }
78 
82  public function assetlog()
83  {
84  return $this->hasMany('\App\Models\Actionlog', 'component_id')->where('asset_type', '=', 'component')->orderBy('created_at', 'desc')->withTrashed();
85  }
86 
87 
88  public function numRemaining()
89  {
90  $checkedout = 0;
91 
92  foreach ($this->assets as $checkout) {
93  $checkedout += $checkout->pivot->assigned_qty;
94  }
95 
96 
97  $total = $this->total_qty;
98  $remaining = $total - $checkedout;
99  return $remaining;
100  }
101 
102 
119  public function scopeTextSearch($query, $search)
120  {
121  $search = explode(' ', $search);
122 
123  return $query->where(function ($query) use ($search) {
124 
125  foreach ($search as $search) {
126  $query->whereHas('category', function ($query) use ($search) {
127  $query->where('categories.name', 'LIKE', '%'.$search.'%');
128  })->orWhere(function ($query) use ($search) {
129  $query->whereHas('company', function ($query) use ($search) {
130  $query->where('companies.name', 'LIKE', '%'.$search.'%');
131  });
132  })->orWhere(function ($query) use ($search) {
133  $query->whereHas('location', function ($query) use ($search) {
134  $query->where('locations.name', 'LIKE', '%'.$search.'%');
135  });
136  })->orWhere('components.name', 'LIKE', '%'.$search.'%')
137  ->orWhere('components.order_number', 'LIKE', '%'.$search.'%')
138  ->orWhere('components.purchase_cost', 'LIKE', '%'.$search.'%')
139  ->orWhere('components.purchase_date', 'LIKE', '%'.$search.'%');
140  }
141  });
142  }
143 
152  public function scopeOrderCategory($query, $order)
153  {
154  return $query->join('categories', 'components.category_id', '=', 'categories.id')->orderBy('categories.name', $order);
155  }
156 
165  public function scopeOrderLocation($query, $order)
166  {
167  return $query->leftJoin('locations', 'components.location_id', '=', 'locations.id')->orderBy('locations.name', $order);
168  }
169 
170 
179  public function scopeOrderCompany($query, $order)
180  {
181  return $query->leftJoin('companies', 'components.company_id', '=', 'companies.id')->orderBy('companies.name', $order);
182  }
183 }
assetlog()
Get action logs for this consumable.
Definition: Component.php:82
scopeOrderCategory($query, $order)
Query builder scope to order on company.
Definition: Component.php:152
Model for Components.
Definition: Component.php:19
$rules
Category validation rules.
Definition: Component.php:31
scopeTextSearch($query, $search)
Query builder scope to search on text.
Definition: Component.php:119
scopeOrderCompany($query, $order)
Query builder scope to order on company.
Definition: Component.php:179
scopeOrderLocation($query, $order)
Query builder scope to order on company.
Definition: Component.php:165