Laravel 8 Multiple Table Relationship

  eloquent, laravel-8, laravel-query-builder, mysql, php

This is a long post, sorry in advance.

I have a couple of questions regarding the diagram below.

table relationship

  1. How do I create a relationship for these tables? (tried "belongsToMany" and "hasMany")
  2. Should I use Eloquent or Query Builder to get desired result? (image is attached below for desired result)
  3. Is "client_order_items" a pivot table?
  4. How can you distinguish a pivot table? Is it because the table has multiple
    foreign keys?
  5. How can I access "images" table from "Manufacturer" model? Can "hasOneThrough / hasManyThrough" achieve this?
  6. Is the desired result achievable purely using Eloquent or DB Query?
//sample:
Manufacturers::find(2)->client_order_items->cars->images->car_image

I tried considering "client_order_items" as a pivot table then making relationships using "belongsToMany" in "Manufacturer", "Car" and "ClientOrder" models.

// AppModelManufacturer.php
public function client_orders() {
    return $this->belongsToMany(ClientOrder::class, 
           "client_order_items", "manufacturer_id", "client_order_id")
           ->withPivot('id', 'quantity', 'car_id');;
}
public function cars() {
    return $this->belongsToMany(Car::class, 
           "client_order_items", "manufacturer_id", "car_id");
}

// AppModelCar.php
public function client_orders() {
    return $this->belongsToMany(ClientOrder::class, 
           "client_order_items", "car_id", "client_order_id");
}
public function manufacturers() {
    return $this->belongsToMany(Manufacturer::class, 
           "client_order_items", "car_id", "manufacturer_id");
}

// AppModelClientOrder.php
public function cars() {
    return $this->belongsToMany(Manufacturer::class, 
           "client_order_items", "client_order_id", "car_id");
}
public function manufacturers() {
    return $this->belongsToMany(Manufacturer::class, 
           "client_order_items", "client_order_id", "manufacturer_id");
}

I also tried "hasMany" relationship:

// AppModelManufacturer.php
public function client_order_items() {
    return $this->hasMany(ClientOrderItems::class);
}
// AppModelCars.php
public function client_order_items() {
    return $this->hasMany(ClientOrderItems::class);
}
// AppModelClientOrder.php
public function client_order_items() {
    return $this->hasMany(ClientOrderItems::class);
}
// AppModelClientOrderItems.php
public function car() {
    return $this->belongsTo(Car::class);
}
public function manufacturer() {
    return $this->belongsTo(Manufacturer::class);
}
public function client_order() {
    return $this->belongsTo(ClientOrder::class);
}

My goal is to get the result if i did something like this:

// Manufacturer::find(2)->client_orders

// Desired Result:
[
    {
        "client_order_id": 88062,
        "first_name": "Clark",
        "last_name": "Kent",
        "order_items": [
            {
                "client_order_item_id": 37394,
                "quantity": 1,
                "car_id": 68,
                "image": "path_img1"
            }
        ]
    },
    {
        "client_order_id": 60978,
        "first_name": "Bruce",
        "last_name": "Wayne",
        "order_items": [
            {
                "client_order_item_id": 79913,
                "quantity": 1,
                "car_id": 68,
                "image": "path_img1"
            },
            {
                "client_order_item_id": 84743,
                "quantity": 1,
                "car_id": 95,
                "image": "path_img2"
            }
        ]
    }
]

But the result I’m currently getting (with "belongsToMany") is:

// Manufacturer::find(2)->client_orders

// Current Result:
[
    {
        "id": 88062,
        "first_name": "Clark",
        "last_name": "Kent",
        "pivot": {
            "manufacturer_id": 2,
            "client_order_id": 88062,
            "id": 37394,
            "quantity": 1,
            "car_id": 68
        }
    },
    {
        "id": 60978,
        "first_name": "Bruce",
        "last_name": "Wayne",
        "pivot": {
            "manufacturer_id": 2,
            "client_order_id": 60978,
            "id": 79913,
            "quantity": 1,
            "car_id": 68
        }
    },
    {
        "id": 60978,
        "first_name": "Bruce",
        "last_name": "Wayne",
        "pivot": {
            "manufacturer_id": 2,
            "client_order_id": 60978,
            "id": 84743,
            "quantity": 1,
            "car_id": 95
        }
    }
]

Sorry again for the long post.
Thank you in advance.

Source: Ask PHP

LEAVE A COMMENT