Attendize/app/Models/Category.php

126 lines
3.7 KiB
PHP

<?php
/**
* Created by PhpStorm.
* User: merdan
* Date: 12/9/2018
* Time: 9:52 PM
*/
namespace App\Models;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;
class Category extends \Illuminate\Database\Eloquent\Model{
use \Backpack\CRUD\CrudTrait;
/**
* 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;
protected $fillable = ['title_tm','title_ru','view_type','lft','rgt','parent_id','depth'];
/**
* 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')};
}
/**
* The events associated with the category.
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function events(){
return $this->hasMany(\App\Models\Event::class);
}
public function cat_events(){
return $this->hasMany(\App\Models\Event::class,'sub_category_id')
->withCount(['stats as views' => function($q){
$q->select(DB::raw("SUM(views) as v"));
}]);
}
public function scopeCategoryLiveEvents($query,$limit){
// dd($this->view_type);
return $query->select('id','title_tm','title_ru','lft')
->orderBy('lft')
->with(['events' => function($q) use($limit){
$q->select('id','title','description','category_id','sub_category_id','start_date')
->limit($limit)
->with('starting_ticket')
->withCount(['stats as views' => function($q){
$q->select(DB::raw("SUM(views) as v"));
}])
->onLive();
}]);
}
public function parent(){
return $this->belongsTo(Category::class,'parent_id');
}
public function children(){
return $this->hasMany(Category::class,'parent_id')
->select('id','title_ru','title_tm','parent_id','lft')
->orderBy('lft');
}
public function scopeMain($query){
return $query->where('depth',1)->orderBy('lft','asc');
}
public function scopeSub($query){
return $query->where('depth',2)->orderBy('lft','asc');
}
public function scopeChildren($query,$parent_id){
return $query->where('parent_id',$parent_id)->orderBy('lft','asc');
}
public function scopeWithLiveEvents($query, $date = false, $popular = true){
$limit = 8;
return $query->with(['cat_events' => function($query) use ($date, $limit, $popular) {
$query->select('id','title','description','category_id','sub_category_id','start_date')
->limit($limit)
->with('starting_ticket')
->withCount(['stats as views' => function($q){
$q->select(DB::raw("SUM(views) as v"));}])
->onLive();//event scope onLive get only live events
if($date)
$query->whereDate('end_date','>=',Carbon::parse($date));
if($popular)
$query->orderBy('views','desc');
else
$query->orderBy('start_date');
}]);
}
}