Giveaway.php 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. <?php
  2. namespace App\Models;
  3. use Illuminate\Database\Eloquent\Model;
  4. class Giveaway extends Model
  5. {
  6. protected $table = 'giveaway';
  7. protected $fillable = [
  8. 'user_id', 'purchaser_user_id', 'title', 'amount', 'order_id', 'created_at', 'updated_at',
  9. ];
  10. public function user()
  11. {
  12. return $this->hasOne(User::class, 'id', 'purchaser_user_id')->select(['id', 'name', 'avatar']);
  13. }
  14. public function buyUser()
  15. {
  16. return $this->hasOne(User::class, 'id', 'user_id')->select(['id', 'name', 'avatar']);
  17. }
  18. public function order()
  19. {
  20. return $this->hasOne(Order::class, 'id', 'order_id');
  21. }
  22. public function getAmountAttribute($value)
  23. {
  24. return $value / 100;
  25. }
  26. public function getCreatedAtAttribute($value)
  27. {
  28. $dateTime = new \DateTime($value);
  29. // 格式化时间
  30. $formattedTime = $dateTime->format('Y-m-d H:i:s');
  31. return $formattedTime;
  32. }
  33. public function getUpdatedAtAttribute($value)
  34. {
  35. $dateTime = new \DateTime($value);
  36. // 格式化时间
  37. $formattedTime = $dateTime->format('Y-m-d H:i:s');
  38. return $formattedTime;
  39. }
  40. }