| Path : /var/www/html/phkaynews-v2/app/Models/ |
|
B-Con CMD Config cPanel C-Rdp D-Log Info Jump Mass Ransom Symlink vHost Zone-H |
| Current File : /var/www/html/phkaynews-v2/app/Models/Post.php |
<?php
namespace App\Models;
use App\Models\User;
use App\Models\Category;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Support\HtmlString;
class Post extends Model
{
use HasFactory;
use SoftDeletes;
protected $table = 'posts';
protected $fillable = [
'title', 'feature_image', 'description', 'user_id', 'view', 'keyword', 'public'
];
protected $primaryKey = 'id';
public function categories()
{
return $this->belongsToMany(Category::class, 'post_categories')->withTimestamps();
}
public function post_categories()
{
return $this->hasMany('App\Models\PostCategory', 'post_id');
}
public function user()
{
return $this->belongsTo(User::class);
}
public function scopeDescendingPost($query)
{
return $query->orderBy('id', 'desc');
}
public function scopeAscendingPost($query)
{
return $query->orderBy('id', 'asc');
}
public function getDateAttribute()
{
Carbon::setLocale('km');
return Carbon::parse($this->attributes['created_at'])->diffForHumans();
}
public function getFullDateAttribute()
{
return date('d-m-Y h:i a', strtotime($this->attributes['created_at']));
}
public function getPostDescriptionAttribute()
{
return new HtmlString($this->attributes['description']);
}
public function scopePubliced($query)
{
return $query->where('public', 1);
}
public static function search($field, $value)
{
if (empty($field)) {
return empty($value) ? static::query() : static::where('title', 'like', '%' . $value . '%');
}
return empty($value) ? static::query() : static::where($field, 'like', '%' . $value . '%');
}
public static function filterDate($value, $title, $user_name)
{
if (is_array($value)) {
if ($title != null || $user_name != null) {
if ($title != null && $user_name != null) {
return static::where('title', 'like', '%' . $title . '%')->whereBetween('created_at', $value)->whereHas('user', function ($query) use ($user_name) {
$query->where('name', 'like', '%' . $user_name . '%');
});
} else {
if (empty($title)) {
return static::whereBetween('created_at', $value)->whereHas('user', function ($query) use ($user_name) {
$query->where('name', 'like', '%' . $user_name . '%');
});
} else {
return static::whereBetween('created_at', $value)->where('title', 'like', '%' . $title . '%');
}
}
} else {
return empty($value) ? static::query() : static::whereBetween('created_at', $value);
}
}
return empty($value) ? static::query() : static::whereDate('created_at', $value);
}
}