Attendize/app/Models/Category.php

75 lines
1.9 KiB
PHP
Raw Normal View History

2018-12-10 07:27:23 +00:00
<?php
/**
* Created by PhpStorm.
* User: merdan
* Date: 12/9/2018
* Time: 9:52 PM
*/
namespace App\Models;
2019-09-14 12:27:41 +00:00
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Str;
2019-08-24 06:55:14 +00:00
class Category extends \Illuminate\Database\Eloquent\Model{
use \Backpack\CRUD\CrudTrait;
2018-12-10 07:27:23 +00:00
/**
* Indicates whether the model should be timestamped.
*
* @var bool $timestamps
*/
public $timestamps = false;
/**
* The database table used by the model.
*
* @var string $table
*/
protected $table = 'categories';
/**
* Indicates whether the model should use soft deletes.
*
* @var bool $softDelete
*/
protected $softDelete = false;
2019-09-10 13:33:55 +00:00
protected $fillable = ['title_tm','title_ru','view_type','lft','rgt','parent_id','depth'];
2019-09-14 12:27:41 +00:00
/**
* Get the url of the event.
*
* @return string
*/
public function getUrlAttribute()
{
return route("showCategoryEventsPage", ["cat_id"=>$this->id, "cat_slug"=>Str::slug($this->title)]);
//return URL::to('/') . '/e/' . $this->id . '/' . Str::slug($this->title);
}
public function getTitleAttribute(){
return $this->{'title_'.Config::get('app.locale')};
}
2018-12-10 07:27:23 +00:00
/**
2019-08-24 06:55:14 +00:00
* The events associated with the category.
2018-12-10 07:27:23 +00:00
*
2019-08-24 06:55:14 +00:00
* @return \Illuminate\Database\Eloquent\Relations\HasMany
2018-12-10 07:27:23 +00:00
*/
2019-08-24 06:55:14 +00:00
public function events(){
return $this->hasMany(\App\Models\Event::class);
}
public function scopeMain($query){
2019-08-28 06:34:37 +00:00
return $query->where('depth',1)->orderBy('lft','asc');
2019-08-24 06:55:14 +00:00
}
public function scopeSub($query){
2019-09-10 13:33:55 +00:00
return $query->where('depth',2)->orderBy('lft','asc');
2019-08-24 06:55:14 +00:00
}
public function scopeChildren($query,$parent_id){
return $query->where('parent_id',$parent_id)->orderBy('lft','asc');
2018-12-10 07:27:23 +00:00
}
2019-09-16 13:30:51 +00:00
public function parent(){
return $this->belongsTo(Category::class,'parent_id');
}
2018-12-10 07:27:23 +00:00
}