fix: correct polymorphic relations in comment, favorite and payment models
This commit is contained in:
+13
-6
@@ -9,6 +9,7 @@ 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
|
||||||
@@ -52,13 +53,19 @@ class Comment extends Model
|
|||||||
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');
|
||||||
|
// }
|
||||||
}
|
}
|
||||||
|
|||||||
+7
-11
@@ -7,8 +7,9 @@
|
|||||||
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
|
||||||
@@ -21,8 +22,7 @@ use Illuminate\Database\Eloquent\Model;
|
|||||||
* @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
|
||||||
*/
|
*/
|
||||||
@@ -42,18 +42,14 @@ class Favorite extends Model
|
|||||||
'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');
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -44,4 +44,9 @@ class Payment extends Model
|
|||||||
'transaction_id',
|
'transaction_id',
|
||||||
'status'
|
'status'
|
||||||
];
|
];
|
||||||
|
|
||||||
|
public function user()
|
||||||
|
{
|
||||||
|
return $this->belongsTo(User::class);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user