Offer.php 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. namespace App\Models;
  3. use App\Models\Item as Item;
  4. use App\User;
  5. use Illuminate\Database\Eloquent\Model;
  6. use Illuminate\Database\Eloquent\SoftDeletes;
  7. class Offer extends Model
  8. {
  9. use SoftDeletes;
  10. protected $dates = ['deleted_at'];
  11. protected $table = "offers";
  12. protected $guarded = ['id'];
  13. protected $fillable = ["item_id", "item_offer_id", 'status_id'];
  14. public function itemId()
  15. {
  16. return $this->hasOne('item', 'id', 'item_id');
  17. }
  18. public function itemOfferId()
  19. {
  20. return $this->hasOne('item', 'id', 'item_offer_id');
  21. }
  22. public function users()
  23. {
  24. return $this->hasManyThrough('App\Item', 'App\User');
  25. }
  26. public function items()
  27. {
  28. return $this->belongsTo(Item::class, 'id')->orWhere('items.id', $this->item_offer_id);
  29. }
  30. public function status()
  31. {
  32. return $this->$this->belongsTo('statuses', 'id', 'status_id');
  33. }
  34. public function getItems()
  35. {
  36. $this->attributes['items'] =[];
  37. // $items = $this->belongsTo(Item::class, 'id')->orWhere('items.id', $this->item_offer_id)->get();
  38. // foreach ($items as $item) {
  39. // if ($this->attributes['item_offer_id'] === $item->id) {
  40. $this->attributes['items'][0] = Item::find($this->attributes['item_id']);
  41. // } else {
  42. $this->attributes['items'][1] = Item::find($this->attributes['item_offer_id']);
  43. // }
  44. // }
  45. return $this->attributes['items'];
  46. // return $this->attributes['items'] = Item::find([$this->attributes['item_id'], $this->attributes['item_offer_id']]);
  47. }
  48. public function getUsers()
  49. {
  50. $this->attributes['users']['seller'] = User::where('items.id', $this->attributes['item_offer_id'])
  51. ->join('items', 'items.user_id', 'users.id')->first();
  52. $this->attributes['users']['buyer'] = User::where('items.id', $this->attributes['item_offer_id'])
  53. ->join('items', 'items.user_id', 'users.id')->first();
  54. return $this->attributes['users'];
  55. }
  56. public function buyer()
  57. {
  58. return $this->hasManyThrough('App\User', 'App\Models\Item', 'user_id', 'item_offer_id');
  59. }
  60. public function seller()
  61. {
  62. return $this->hasManyThrough('App\User', 'App\Models\Item', 'user_id', 'item_id');
  63. }
  64. public function __get($key)
  65. {
  66. if ($key === "items") {
  67. return $this->attributes['items'] ?? $this->getItems();
  68. }
  69. if ($key === "users") {
  70. return $this->attributes['users'] ?? $this->getUsers();
  71. }
  72. return parent::__get($key);
  73. }
  74. }