|
1
|
|
|
<?php namespace App\Models; |
|
2
|
|
|
|
|
3
|
|
|
use Illuminate\Database\Eloquent\SoftDeletes; |
|
4
|
|
|
use Illuminate\Database\Eloquent\Model; |
|
5
|
|
|
use Illuminate\Support\Facades\DB; |
|
6
|
|
|
use \Session; |
|
7
|
|
|
|
|
8
|
|
|
class Categories extends Model { |
|
9
|
|
|
|
|
10
|
|
|
use SoftDeletes; |
|
11
|
|
|
|
|
12
|
|
|
protected $table = 'categories'; |
|
13
|
|
|
public $timestamps = true; |
|
14
|
|
|
protected $dates = ['deleted_at']; |
|
15
|
|
|
|
|
16
|
|
|
protected $guarded = ['category_id']; |
|
17
|
|
|
protected $fillable = ['group_id', 'category_title', 'description', 'is_active']; |
|
18
|
|
|
|
|
19
|
|
|
public function getKeyName(){ |
|
20
|
|
|
|
|
21
|
|
|
return 'category_id'; |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Selects all the categories from the current group |
|
26
|
|
|
* |
|
27
|
|
|
* Allows to extend the query (scope) to only selects the categories from the group |
|
28
|
|
|
* where the user is connected. |
|
29
|
|
|
* @example Categories::group(); |
|
30
|
|
|
* |
|
31
|
|
|
* @param Object [Laravel] |
|
32
|
|
|
*/ |
|
33
|
|
|
public function scopeFromGroup($query) { |
|
34
|
|
|
|
|
35
|
|
|
return $query->where('group_id', Session::get('groupID') ); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Selects all the products from one category |
|
40
|
|
|
* |
|
41
|
|
|
* Easily fetch all the products that belongs to a category. Watch out, |
|
42
|
|
|
* only works for ONE category. |
|
43
|
|
|
* @example Categories::find(1)->products; |
|
44
|
|
|
* |
|
45
|
|
|
* @return Object [Laravel] |
|
46
|
|
|
*/ |
|
47
|
|
|
public function products() { |
|
48
|
|
|
|
|
49
|
|
|
return $this->hasMany('App\Models\Products', 'category_id', 'category_id'); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
} |