update
This commit is contained in:
53
app/Models/Comments.php
Normal file
53
app/Models/Comments.php
Normal file
@ -0,0 +1,53 @@
|
||||
<?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\BelongsTo;
|
||||
|
||||
class Comments extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
|
||||
protected $primaryKey = 'id';
|
||||
public $timestamps = true;
|
||||
protected $fillable = [
|
||||
'parent_id',
|
||||
'users_id',
|
||||
'news_id',
|
||||
'content',
|
||||
'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>';
|
||||
}
|
||||
|
||||
|
||||
public function news():BelongsTo{
|
||||
|
||||
return $this->belongsTo(News::class,'news_id','news_id');
|
||||
}
|
||||
|
||||
public function parent(){
|
||||
return $this->belongsTo(Comments::class,'parent_id');
|
||||
}
|
||||
|
||||
public function subComments(){
|
||||
return $this->hasMany(Comments::class,'parent_id');
|
||||
}
|
||||
|
||||
public function user(){
|
||||
return $this->belongsTo(User::class, 'users_id', 'id');
|
||||
}
|
||||
|
||||
}
|
@ -89,4 +89,12 @@ class News extends Model
|
||||
public function provinces(): BelongsTo{
|
||||
return $this->belongsTo(Provinces::class, 'provinces_id', 'province_id');
|
||||
}
|
||||
|
||||
public function comments()
|
||||
{
|
||||
return $this->hasMany(Comments::class, 'news_id', 'news_id')->with('subcomments')
|
||||
->where('parent_id', 0)
|
||||
->orWhere('parent_id', null)
|
||||
->where('status', 1);
|
||||
}
|
||||
}
|
||||
|
@ -45,4 +45,8 @@ class User extends Authenticatable
|
||||
'email_verified_at' => 'datetime',
|
||||
'password' => 'hashed',
|
||||
];
|
||||
|
||||
public function comments(){
|
||||
return $this->hasMany(Comments::class,'users_id','id');
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user