fix: correct polymorphic relations in comment, favorite and payment models

This commit is contained in:
2026-08-17 08:13:02 +03:30
parent 42be8ce894
commit ed02b45269
3 changed files with 82 additions and 74 deletions
+35 -28
View File
@@ -9,10 +9,11 @@ namespace App\Models;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\MorphTo;
/**
* Class Comment
*
*
* @property int $id
* @property int $user_id
* @property int $commentable_id
@@ -21,7 +22,7 @@ use Illuminate\Database\Eloquent\Model;
* @property int $status
* @property Carbon $created_at
* @property Carbon $updated_at
*
*
* @property User $user
* @property Comment $comment
* @property Collection|Comment[] $comments
@@ -30,35 +31,41 @@ use Illuminate\Database\Eloquent\Model;
*/
class Comment extends Model
{
protected $table = 'comments';
public static $snakeAttributes = false;
protected $table = 'comments';
public static $snakeAttributes = false;
protected $casts = [
'user_id' => 'int',
'commentable_id' => 'int',
'status' => 'int'
];
protected $casts = [
'user_id' => 'int',
'commentable_id' => 'int',
'status' => 'int'
];
protected $fillable = [
'user_id',
'commentable_id',
'commentable_type',
'body',
'status'
];
protected $fillable = [
'user_id',
'commentable_id',
'commentable_type',
'body',
'status'
];
public function user()
{
return $this->belongsTo(User::class);
}
public function user()
{
return $this->belongsTo(User::class);
}
public function comment()
{
return $this->belongsTo(Comment::class, 'commentable_id');
}
// ✅ رابطه پلیمورفیک: نظر میتونه متعلق به محصول، خبر، پست و... باشه
public function commentable(): MorphTo
{
return $this->morphTo();
}
public function comments()
{
return $this->hasMany(Comment::class, 'commentable_id');
}
// public function comment()
// {
// return $this->belongsTo(Comment::class, 'commentable_id');
// }
//
// public function comments()
// {
// return $this->hasMany(Comment::class, 'commentable_id');
// }
}