1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889 |
- <?php
- namespace App\Models;
- use App\Models\Item as Item;
- use App\User;
- use Illuminate\Database\Eloquent\Model;
- use Illuminate\Database\Eloquent\SoftDeletes;
- class Offer extends Model
- {
- use SoftDeletes;
- protected $dates = ['deleted_at'];
- protected $table = "offers";
- protected $guarded = ['id'];
- protected $fillable = ["item_id", "item_offer_id", 'status_id'];
- public function itemId()
- {
- return $this->hasOne('item', 'id', 'item_id');
- }
- public function itemOfferId()
- {
- return $this->hasOne('item', 'id', 'item_offer_id');
- }
- public function users()
- {
- return $this->hasManyThrough('App\Item', 'App\User');
- }
- public function items()
- {
- return $this->belongsTo(Item::class, 'id')->orWhere('items.id', $this->item_offer_id);
- }
- public function status()
- {
- return $this->$this->belongsTo('statuses', 'id', 'status_id');
- }
- public function getItems()
- {
- $this->attributes['items'] =[];
- // $items = $this->belongsTo(Item::class, 'id')->orWhere('items.id', $this->item_offer_id)->get();
- // foreach ($items as $item) {
- // if ($this->attributes['item_offer_id'] === $item->id) {
- $this->attributes['items'][0] = Item::find($this->attributes['item_id']);
- // } else {
- $this->attributes['items'][1] = Item::find($this->attributes['item_offer_id']);
- // }
- // }
- return $this->attributes['items'];
- // return $this->attributes['items'] = Item::find([$this->attributes['item_id'], $this->attributes['item_offer_id']]);
- }
- public function getUsers()
- {
- $this->attributes['users']['seller'] = User::where('items.id', $this->attributes['item_offer_id'])
- ->join('items', 'items.user_id', 'users.id')->first();
- $this->attributes['users']['buyer'] = User::where('items.id', $this->attributes['item_offer_id'])
- ->join('items', 'items.user_id', 'users.id')->first();
- return $this->attributes['users'];
- }
- public function buyer()
- {
- return $this->hasManyThrough('App\User', 'App\Models\Item', 'user_id', 'item_offer_id');
- }
- public function seller()
- {
- return $this->hasManyThrough('App\User', 'App\Models\Item', 'user_id', 'item_id');
- }
- public function __get($key)
- {
- if ($key === "items") {
- return $this->attributes['items'] ?? $this->getItems();
- }
- if ($key === "users") {
- return $this->attributes['users'] ?? $this->getUsers();
- }
- return parent::__get($key);
- }
- }
|