2018-08-23 00:01:54 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
|
|
|
|
|
|
|
class Category extends Model
|
|
|
|
{
|
|
|
|
use SoftDeletes;
|
|
|
|
|
|
|
|
public $table = 'categories';
|
|
|
|
|
|
|
|
public $fillable = [
|
|
|
|
'user_id',
|
|
|
|
'name',
|
|
|
|
'description',
|
|
|
|
'parent_category',
|
|
|
|
'is_private',
|
|
|
|
];
|
|
|
|
|
|
|
|
/*
|
|
|
|
| ========================================================================
|
|
|
|
| SCOPES
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Scope for the user relation
|
|
|
|
*
|
|
|
|
* @param $query
|
|
|
|
* @param int $user_id
|
|
|
|
* @return mixed
|
|
|
|
*/
|
2018-08-23 01:09:31 +02:00
|
|
|
public function scopeByUser($query, $user_id)
|
2018-08-23 00:01:54 +02:00
|
|
|
{
|
|
|
|
return $query->where('user_id', $user_id);
|
|
|
|
}
|
|
|
|
|
2018-08-23 10:37:48 +02:00
|
|
|
/**
|
|
|
|
* @param $query
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function scopeParentOnly($query)
|
|
|
|
{
|
|
|
|
return $query->where('parent_category', null);
|
|
|
|
}
|
|
|
|
|
2018-08-23 00:01:54 +02:00
|
|
|
/*
|
|
|
|
| ========================================================================
|
|
|
|
| RELATIONSHIPS
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
|
|
|
*/
|
|
|
|
public function user()
|
|
|
|
{
|
|
|
|
return $this->belongsTo('App\Models\User', 'user_id');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany
|
|
|
|
*/
|
|
|
|
public function links()
|
|
|
|
{
|
|
|
|
return $this->hasMany('App\Models\Link', 'category_id');
|
|
|
|
}
|
2018-08-23 10:37:48 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany
|
|
|
|
*/
|
|
|
|
public function childCategories()
|
|
|
|
{
|
|
|
|
return $this->hasMany('App\Models\Category', 'parent_category');
|
|
|
|
}
|
2018-08-23 00:01:54 +02:00
|
|
|
}
|