AssetModel.php
Go to the documentation of this file.
1 <?php
2 namespace App\Models;
3 
7 
14 class AssetModel extends Model
15 {
16  use SoftDeletes;
17  protected $dates = ['deleted_at'];
18  protected $table = 'models';
19 
20  // Declare the rules for the model validation
21  protected $rules = array(
22  'name' => 'required|min:2|max:255',
23  'modelno' => 'min:1|max:255',
24  'category_id' => 'required|integer',
25  'manufacturer_id' => 'required|integer',
26  'eol' => 'integer:min:0|max:240',
27  'user_id' => 'integer',
28  );
29 
37  protected $injectUniqueIdentifier = true;
38  use ValidatingTrait;
39 
45  protected $fillable = ['name','manufacturer_id','category_id','eol'];
46 
47 
48 
49  public function assets()
50  {
51  return $this->hasMany('\App\Models\Asset', 'model_id');
52  }
53 
54  public function category()
55  {
56  return $this->belongsTo('\App\Models\Category', 'category_id');
57  }
58 
59  public function depreciation()
60  {
61  return $this->belongsTo('\App\Models\Depreciation', 'depreciation_id');
62  }
63 
64  public function adminuser()
65  {
66  return $this->belongsTo('\App\Models\User', 'user_id');
67  }
68 
69  public function manufacturer()
70  {
71  return $this->belongsTo('\App\Models\Manufacturer', 'manufacturer_id');
72  }
73 
74  public function fieldset()
75  {
76  return $this->belongsTo('\App\Models\CustomFieldset', 'fieldset_id');
77  }
78 
79  public function getNote()
80  {
81 
82  $Parsedown = new \Parsedown();
83 
84  if ($this->note) {
85  return $Parsedown->text(e($this->note));
86  }
87 
88  }
89 
103  public function scopeDeleted($query)
104  {
105  return $query->whereNotNull('deleted_at');
106  }
107 
119  public function scopeInCategory($query, array $categoryIdListing)
120  {
121 
122  return $query->whereIn('category_id', $categoryIdListing);
123  }
124 
133  public function scopeTextSearch($query, $search)
134  {
135 
136  return $query->where('name', 'LIKE', "%$search%")
137  ->orWhere('modelno', 'LIKE', "%$search%")
138  ->orWhere(function ($query) use ($search) {
139  $query->whereHas('depreciation', function ($query) use ($search) {
140  $query->where('name', 'LIKE', '%'.$search.'%');
141  });
142  })
143  ->orWhere(function ($query) use ($search) {
144  $query->whereHas('category', function ($query) use ($search) {
145  $query->where('name', 'LIKE', '%'.$search.'%');
146  });
147  })
148  ->orWhere(function ($query) use ($search) {
149  $query->whereHas('manufacturer', function ($query) use ($search) {
150  $query->where('name', 'LIKE', '%'.$search.'%');
151  });
152  });
153 
154  }
155 }
scopeTextSearch($query, $search)
Query builder scope to search on text.
Definition: AssetModel.php:133
Model for Asset Models.
Definition: AssetModel.php:14
scopeInCategory($query, array $categoryIdListing)
scopeInCategory Get all models that are in the array of category ids
Definition: AssetModel.php:119
scopeDeleted($query)
BEGIN QUERY SCOPES
Definition: AssetModel.php:103