How to add a model into another model in laravel?

24 Views Asked by At

I am not so much experienced in laravel. I am creating an order managment dashboard and i am kinda stuck here. I want to use best practice here to achieve the results. I have 5 tables (schemas are simplified):

1. User (id, username, email)
2. Orders (id, user_id, order_type)
3. vector_order(id, order_id) 
4. digitizing_order(id, order_id)
5. graphics_order(id, order_id)

where in table 3, 4, and 5 Foreign Key(order_id) references Order(id). Orders has one-to-one relationship with vector, graphics and digitizing tables.

To get details of an order, i first need to query table orders and then based on the value of order_type (which is numeric based 0, 1, 2) I need to fetch further details about that order from tables defined in point 3, 4, 5. To get the user info of the order i am using OrderModel::with('user') method which is working fine if i only fetch Order + user info but i am unable to get the details from tables vector_order, digitizing_order and graphic_order that way.

Right now, i am doing it this way:

public function getOrderDetails($orderId){
    $orderInfo = OrderModel::with('user')->find($orderId);
    $detail = [$orderInfo];

    switch($orderInfo->order_type){
       case 0:
           array_push($detail, VectorModel::find($job->id));
       case 1:
           array_push($detail, DigitizingModel::find($job->id));
       case 2:
           array_push($detail, GraphicModel::find($job->id));
       default:
           break;
    }

    return $detail;

This doesn't seem to be the correct way. Please, help me write it in better way.

0

There are 0 best solutions below