Completed
Push — master ( 6fa307...fa7e27 )
by Corentin
04:31
created

Categories   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 4
Bugs 1 Features 0
Metric Value
wmc 3
c 4
b 1
f 0
lcom 0
cbo 2
dl 0
loc 45
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getKeyName() 0 4 1
A scopeFromGroup() 0 4 1
A products() 0 4 1
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
}