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

Categories::allAPI()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 10
rs 9.4285
cc 1
eloc 3
nc 1
nop 0
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
}