Blame view

app/Models/Model.php 2.85 KB
b7c7a5f6   Alexey Boroda   first commit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
  <?php
  
  namespace App\Models;
  
  use App\Scopes\Company;
  use EloquentFilter\Filterable;
  use Illuminate\Database\Eloquent\Model as Eloquent;
  use Illuminate\Database\Eloquent\SoftDeletes;
  use Kyslik\ColumnSortable\Sortable;
  use Request;
  use Route;
  
  class Model extends Eloquent
  {
      use Filterable, SoftDeletes, Sortable;
  
      protected $dates = ['deleted_at'];
  
      /**
       * The "booting" method of the model.
       *
       * @return void
       */
      protected static function boot()
      {
          parent::boot();
  
          static::addGlobalScope(new Company);
      }
  
      /**
       * Global company relation.
       *
       * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
       */
      public function company()
      {
          return $this->belongsTo('App\Models\Common\Company');
      }
  
      /**
       * Define the filter provider globally.
       *
       * @return ModelFilter
       */
      public function modelFilter()
      {
          // Check if is api or web
          if (Request::is('api/*')) {
              $arr = array_reverse(explode('\\', explode('@', app()['api.router']->currentRouteAction())[0]));
              $folder = $arr[1];
              $file = $arr[0];
          } else {
              list($folder, $file) = explode('/', Route::current()->uri());
          }
  
          if (empty($folder) || empty($file)) {
              return $this->provideFilter();
          }
  
          $class = '\App\Filters\\' . ucfirst($folder) . '\\' . ucfirst($file);
  
          return $this->provideFilter($class);
      }
  
      /**
       * Scope to only include company data.
       *
       * @param \Illuminate\Database\Eloquent\Builder $query
       * @param $company_id
       *
       * @return \Illuminate\Database\Eloquent\Builder
       */
      public function scopeCompanyId($query, $company_id)
      {
          return $query->where($this->table . '.company_id', '=', $company_id);
      }
  
      /**
       * Scope to get all rows filtered, sorted and paginated.
       *
       * @param \Illuminate\Database\Eloquent\Builder $query
       * @param $sort
       *
       * @return \Illuminate\Database\Eloquent\Builder
       */
      public function scopeCollect($query, $sort = 'name')
      {
          $request = request();
  
          $input = $request->input();
          $limit = $request->get('limit', setting('general.list_limit', '25'));
  
          return $query->filter($input)->sortable($sort)->paginate($limit);
      }
  
      /**
       * Scope to only include active models.
       *
       * @param \Illuminate\Database\Eloquent\Builder $query
       * @return \Illuminate\Database\Eloquent\Builder
       */
      public function scopeEnabled($query)
      {
          return $query->where('enabled', 1);
      }
  
      /**
       * Scope to only include passive models.
       *
       * @param \Illuminate\Database\Eloquent\Builder $query
       * @return \Illuminate\Database\Eloquent\Builder
       */
      public function scopeDisabled($query)
      {
          return $query->where('enabled', 0);
      }
  }