From ed02b452693f0dd78c1a00e939afa5f513ca7f8c Mon Sep 17 00:00:00 2001 From: shahrzad011 Date: Mon, 17 Aug 2026 08:13:02 +0330 Subject: [PATCH] fix: correct polymorphic relations in comment, favorite and payment models --- app/Models/Comment.php | 63 +++++++++++++++++++++++------------------ app/Models/Favorite.php | 54 ++++++++++++++++------------------- app/Models/Payment.php | 39 ++++++++++++++----------- 3 files changed, 82 insertions(+), 74 deletions(-) diff --git a/app/Models/Comment.php b/app/Models/Comment.php index 37f7037..5fa55a0 100644 --- a/app/Models/Comment.php +++ b/app/Models/Comment.php @@ -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'); +// } } diff --git a/app/Models/Favorite.php b/app/Models/Favorite.php index 391d9c1..c055108 100644 --- a/app/Models/Favorite.php +++ b/app/Models/Favorite.php @@ -7,53 +7,49 @@ namespace App\Models; use Carbon\Carbon; -use Illuminate\Database\Eloquent\Collection; use Illuminate\Database\Eloquent\Model; +use Illuminate\Database\Eloquent\Relations\MorphTo; +use Illuminate\Database\Eloquent\Relations\BelongsTo; /** * Class Favorite - * + * * @property int $id * @property int $user_id * @property int $favoritable_id * @property string $favoritable_type * @property Carbon $created_at * @property Carbon $updated_at - * + * * @property User $user - * @property Favorite $favorite - * @property Collection|Favorite[] $favorites + * @property Model $favoritable * * @package App\Models */ class Favorite extends Model { - protected $table = 'favorites'; - public static $snakeAttributes = false; + protected $table = 'favorites'; + public static $snakeAttributes = false; - protected $casts = [ - 'user_id' => 'int', - 'favoritable_id' => 'int' - ]; + protected $casts = [ + 'user_id' => 'int', + 'favoritable_id' => 'int' + ]; - protected $fillable = [ - 'user_id', - 'favoritable_id', - 'favoritable_type' - ]; + protected $fillable = [ + 'user_id', + 'favoritable_id', + 'favoritable_type' + ]; - public function user() - { - return $this->belongsTo(User::class); - } + public function user(): BelongsTo + { + return $this->belongsTo(User::class); + } - public function favorite() - { - return $this->belongsTo(Favorite::class, 'favoritable_id'); - } - - public function favorites() - { - return $this->hasMany(Favorite::class, 'favoritable_id'); - } + // رابطه پلیمورفیک: علاقهمندی میتونه به محصول، خبر، پست و... وصل باشه + public function favoritable(): MorphTo + { + return $this->morphTo(); + } } diff --git a/app/Models/Payment.php b/app/Models/Payment.php index 7c6a0e2..4f3a73e 100644 --- a/app/Models/Payment.php +++ b/app/Models/Payment.php @@ -11,7 +11,7 @@ use Illuminate\Database\Eloquent\Model; /** * Class Payment - * + * * @property int $id * @property int $user_id * @property int $payable_id @@ -26,22 +26,27 @@ use Illuminate\Database\Eloquent\Model; */ class Payment extends Model { - protected $table = 'payments'; - public static $snakeAttributes = false; + protected $table = 'payments'; + public static $snakeAttributes = false; - protected $casts = [ - 'user_id' => 'int', - 'payable_id' => 'int', - 'amount' => 'float', - 'status' => 'int' - ]; + protected $casts = [ + 'user_id' => 'int', + 'payable_id' => 'int', + 'amount' => 'float', + 'status' => 'int' + ]; - protected $fillable = [ - 'user_id', - 'payable_id', - 'amount', - 'authority', - 'transaction_id', - 'status' - ]; + protected $fillable = [ + 'user_id', + 'payable_id', + 'amount', + 'authority', + 'transaction_id', + 'status' + ]; + + public function user() + { + return $this->belongsTo(User::class); + } }