/
home
/
henzagold
/
server
/
app
/
Models
/
File Upload :
llllll
Current File: /home/henzagold/server/app/Models/Category.php
<?php namespace App\Models; use App\DB\Selector; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\SoftDeletes; class Category extends Model { use HasFactory; use SoftDeletes; use Selector; protected $fillable = [ 'title', 'parent_id_id', 'priority', 'image_id', 'meta', 'body', 'icon_id', 'disabled', 'type', 'total', 'is_master', 'header', 'is_active' ]; //protected $connection = 'pgsql'; protected $table = 'category_collection'; protected $hidden = [ 'body' ]; public static $TYPES = [ 1 => "سایت", 2 => "موبایل", 3 => 'عمومی' ]; protected $casts = [ 'disabled' => 'boolean', 'is_active' => 'boolean', ]; public function parent() { return $this->hasOne('App\Models\Category', 'id', 'parent_id_id'); // $this->belongsTo('App\Models\Category','parent_id_id'); } public function childs() { return $this->hasMany('App\Models\Category', 'parent_id_id'); //return $this->hasOne('App\Models\Category', 'id','parent_id_id'); } public function image() { return $this->belongsTo('App\Models\Image', 'image_id'); } public function icon() { return $this->belongsTo('App\Models\Image', 'icon_id'); } public function scopeDesktop($query) { return $query->whereIn('type', [1, 3]); } public function scopeMobile($query) { return $query->whereIn('type', [2, 3])->where('is_active',true); } public static function reloadTotal($id) { return true; } public static function reloadCategory($cats, $pid) { // foreach ($cats ?? [] as $cat) { // $c = Collect::firstOrCreate(['collection_id_id' => $cat, 'product_id_id' => $pid]); // $count = Collect::where(['collection_id_id' => $c->id])->count(); // Category::where('id', $c->id)->update(['total' => $count]); // } // Collect::whereNotIn('collection_id_id',$cats ?? [])->where('product_id_id', $pid)->delete(); // return true; foreach ($cats ?? [] as $cat) { $c = Collect::firstOrCreate(['collection_id_id' => $cat, 'product_id_id' => $pid]); $count = Collect::where(['collection_id_id' => $c->collection_id_id])->count(); Category::where('id',$c->collection_id_id)->update(['total' => $count]); } $collect = Collect::whereNotIn('collection_id_id',$cats ?? [])->where('product_id_id', $pid)->get(); foreach ($collect as $item){ $category = Category::find($item->collection_id_id); if (!is_null($category)){ $category->decrement('total'); } $item->delete(); } return true; } }
Copyright ©2k19 -
Hexid
|
Tex7ure