master_template/app/Models/Newscategories.php
2024-06-15 22:23:54 +05:45

86 lines
2.7 KiB
PHP

<?php
namespace App\Models;
use App\Models\User;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use App\Traits\CreatedUpdatedBy;
use Illuminate\Database\Eloquent\Relations\HasMany;
class Newscategories extends Model
{
use HasFactory, CreatedUpdatedBy;
protected $primaryKey = 'category_id';
public $timestamps = true;
protected $fillable = [
'title',
'nepali_title',
'alias',
'parent_category',
'display_order',
'status',
'remarks',
'createdby',
'updatedby',
'created_at',
'updated_at',
];
protected $appends = ['status_name'];
protected function getStatusNameAttribute()
{
return $this->status == 1 ? '<span class="badge text-bg-success-soft"> Active </span>' : '<span class="badge text-bg-danger-soft">Inactive</span>';
}
protected function createdBy(): Attribute
{
return Attribute::make(
get: fn ($value) => User::find($value) ? User::find($value)->name : '',
);
}
protected function updatedBy(): Attribute
{
return Attribute::make(
get: fn ($value) => User::find($value) ? User::find($value)->name : '',
);
}
public function parent()
{
return $this->belongsTo(Newscategories::class, 'parent_category')->where('status',1);
}
public function children()
{
return $this->hasMany(Newscategories::class, 'parent_category')->where('status',1);
}
public function news(){
return $this->hasMany(News::class, 'newscategories_id', 'category_id')->orderBy('display_order');
}
public function entertainmentNews(){
return $this->hasMany(News::class, 'newscategories_id', 'category_id')->limit(6)->inRandomOrder()->orderBy('display_order');
}
public function technologyNews(){
return $this->hasMany(News::class, 'newscategories_id', 'category_id')->limit(6)->inRandomOrder()->orderBy('display_order');
}
public function culturalNews(){
return $this->hasMany(News::class, 'newscategories_id', 'category_id')->limit(6)->inRandomOrder()->orderBy('display_order');
}
public function sportNews(){
return $this->hasMany(News::class, 'newscategories_id', 'category_id')->limit(9)->inRandomOrder()->orderBy('display_order');
}
public function interviewNews(){
return $this->hasMany(News::class, 'newscategories_id', 'category_id')->limit(6)->inRandomOrder()->orderBy('display_order');
}
public function politicNews(){
return $this->hasMany(News::class, 'newscategories_id', 'category_id')->limit(5)->inRandomOrder()->orderBy('display_order');
}
}