E
E
Eugene2021-08-17 18:19:39
Laravel
Eugene, 2021-08-17 18:19:39

I can't make a correct query to get data through an intermediate table?

DB Structure
ORDERS:
id
status_id
.....


ORDER_ITEMS:
id
order_id
.....


SCHEDULE_SLOTS
id
order_item_id
......

How can I make the SCHEDULE_SLOTS model related to ORDERS through ORDER_ITEMS ?
// Отношение «один-к-одному» без проблем
public function getOrderItem() {
    return $this->hasOne('ModelOrderItem', 'id', 'order_item_id');
}

// А вот тут начинаются трудности
public function getOrder() {
    return $this->hasOneThrough('ModelOrder', 'ModelOrderItem',  ..... );
}


In fact, I can get the information itself if I specify the relation in ORDER_ITEMS :
public function getOrderItem() {
    return $this->hasOne('ModelOrderItem', 'id', 'order_item_id')->with(['getOrder']);
}

But my final task is to make a selection of SCHEDULE_SLOTS , specifying the conditions for the fields in ORDERS . Those. Roughly speaking, I have such a selection code, but there are no getOrder relations themselves.
$returnData = ModelScheduleSlots::with(['getOrderItem', 'getOrder'])
            ->whereHas('getOrder', function ($q) {
                $q->where('status_id', 1);
            });


Well, or until it's too late, the DB structure can be corrected. The main thing is to understand what.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question