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 Carbon\Carbon;
use Illuminate\Database\Eloquent\Collection; use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\MorphTo;
/** /**
* Class Comment * Class Comment
* *
* @property int $id * @property int $id
* @property int $user_id * @property int $user_id
* @property int $commentable_id * @property int $commentable_id
@@ -21,7 +22,7 @@ use Illuminate\Database\Eloquent\Model;
* @property int $status * @property int $status
* @property Carbon $created_at * @property Carbon $created_at
* @property Carbon $updated_at * @property Carbon $updated_at
* *
* @property User $user * @property User $user
* @property Comment $comment * @property Comment $comment
* @property Collection|Comment[] $comments * @property Collection|Comment[] $comments
@@ -30,35 +31,41 @@ use Illuminate\Database\Eloquent\Model;
*/ */
class Comment extends Model class Comment extends Model
{ {
protected $table = 'comments'; protected $table = 'comments';
public static $snakeAttributes = false; public static $snakeAttributes = false;
protected $casts = [ protected $casts = [
'user_id' => 'int', 'user_id' => 'int',
'commentable_id' => 'int', 'commentable_id' => 'int',
'status' => 'int' 'status' => 'int'
]; ];
protected $fillable = [ protected $fillable = [
'user_id', 'user_id',
'commentable_id', 'commentable_id',
'commentable_type', 'commentable_type',
'body', 'body',
'status' 'status'
]; ];
public function user() public function user()
{ {
return $this->belongsTo(User::class); return $this->belongsTo(User::class);
} }
public function comment() // ✅ رابطه پلیمورفیک: نظر میتونه متعلق به محصول، خبر، پست و... باشه
{ public function commentable(): MorphTo
return $this->belongsTo(Comment::class, 'commentable_id'); {
} return $this->morphTo();
}
public function comments() // public function comment()
{ // {
return $this->hasMany(Comment::class, 'commentable_id'); // return $this->belongsTo(Comment::class, 'commentable_id');
} // }
//
// public function comments()
// {
// return $this->hasMany(Comment::class, 'commentable_id');
// }
} }
+25 -29
View File
@@ -7,53 +7,49 @@
namespace App\Models; namespace App\Models;
use Carbon\Carbon; use Carbon\Carbon;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\MorphTo;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
/** /**
* Class Favorite * Class Favorite
* *
* @property int $id * @property int $id
* @property int $user_id * @property int $user_id
* @property int $favoritable_id * @property int $favoritable_id
* @property string $favoritable_type * @property string $favoritable_type
* @property Carbon $created_at * @property Carbon $created_at
* @property Carbon $updated_at * @property Carbon $updated_at
* *
* @property User $user * @property User $user
* @property Favorite $favorite * @property Model $favoritable
* @property Collection|Favorite[] $favorites
* *
* @package App\Models * @package App\Models
*/ */
class Favorite extends Model class Favorite extends Model
{ {
protected $table = 'favorites'; protected $table = 'favorites';
public static $snakeAttributes = false; public static $snakeAttributes = false;
protected $casts = [ protected $casts = [
'user_id' => 'int', 'user_id' => 'int',
'favoritable_id' => 'int' 'favoritable_id' => 'int'
]; ];
protected $fillable = [ protected $fillable = [
'user_id', 'user_id',
'favoritable_id', 'favoritable_id',
'favoritable_type' 'favoritable_type'
]; ];
public function user() public function user(): BelongsTo
{ {
return $this->belongsTo(User::class); return $this->belongsTo(User::class);
} }
public function favorite() // رابطه پلیمورفیک: علاقهمندی میتونه به محصول، خبر، پست و... وصل باشه
{ public function favoritable(): MorphTo
return $this->belongsTo(Favorite::class, 'favoritable_id'); {
} return $this->morphTo();
}
public function favorites()
{
return $this->hasMany(Favorite::class, 'favoritable_id');
}
} }
+22 -17
View File
@@ -11,7 +11,7 @@ use Illuminate\Database\Eloquent\Model;
/** /**
* Class Payment * Class Payment
* *
* @property int $id * @property int $id
* @property int $user_id * @property int $user_id
* @property int $payable_id * @property int $payable_id
@@ -26,22 +26,27 @@ use Illuminate\Database\Eloquent\Model;
*/ */
class Payment extends Model class Payment extends Model
{ {
protected $table = 'payments'; protected $table = 'payments';
public static $snakeAttributes = false; public static $snakeAttributes = false;
protected $casts = [ protected $casts = [
'user_id' => 'int', 'user_id' => 'int',
'payable_id' => 'int', 'payable_id' => 'int',
'amount' => 'float', 'amount' => 'float',
'status' => 'int' 'status' => 'int'
]; ];
protected $fillable = [ protected $fillable = [
'user_id', 'user_id',
'payable_id', 'payable_id',
'amount', 'amount',
'authority', 'authority',
'transaction_id', 'transaction_id',
'status' 'status'
]; ];
public function user()
{
return $this->belongsTo(User::class);
}
} }