staudenmeir / eloquent-has-many-deep

Laravel Eloquent HasManyThrough relationships with unlimited levels
MIT License
2.67k stars 157 forks source link

Many to Many -> Many To many with custom key and several problems #126

Closed keithyuk closed 3 years ago

keithyuk commented 3 years ago

At the very beginnings , i want to ask how to build a relation with many to many->many to many with custom key. I know that there is instruction with [ class User extends Model { use \Staudenmeir\EloquentHasManyDeep\HasRelationships;

public function permissions()
{
    return $this->hasManyDeep('App\Permission', ['role_user', 'App\Role', 'permission_role']);
}

} ] But it does not has custom key . I want to know the full version.

Also , i have another another problems with the following models. I want to create the following relationship. but i dont know how to do. Product ----->belongs To (from_port_id)------> LogisticPort ----->belongs To many(to_port_id) ---->LogistoPort Also, With an Order belongsTo Many Products, Order Belongs To Port with 'to_port_id' ,Order Belongs To Logistic with 'logistic_id', Product belongs To Port with 'from_port_id',

from_port_id , to_port_id ,logistic_id and unit_price combined with a pivot table .

And now I want to ask. How to retrieve an order with different products with corresponding from_port_id,to_port_id,logistic_id to get different unit_price with the eager load as little sql as possible?

Order Model

+--------------+-------------+------+-----+---------+----------------+
| Field        | Type        | Null | Key | Default | Extra          |
+-----------+----------------+------+-----+---------+----------------+
| id           | int(11)     | NO   | PRI | NULL    | auto_increment |
| to_port_id   | int(11)     | NO   |     | NULL    |                |
| logistic_id  | int(11)     | NO   |     | NULL    |                |
+--------------+-------------+------+-----+---------+----------------+

Product Model

+--------------+-------------+------+-----+---------+----------------+
| Field        | Type        | Null | Key | Default | Extra          |
+-----------+----------------+------+-----+---------+----------------+
| id           | int(11)     | NO   | PRI | NULL    | auto_increment |
| from_port_id | int(11)     | NO   |     | NULL    |                |
+--------------+-------------+------+-----+---------+----------------+

LogisticPort PivotModel

+--------------+-------------+------+-----+---------+----------------+
| Field        | Type        | Null | Key | Default | Extra          |
+--------------+-------------+------+-----+---------+----------------+
| from_port_id | int(11)     | NO   | PRI | NULL    |                |
| to_port_id   | int(11)     | NO   | PRI | NULL    |                |
| logistic_id  | int(11)     | NO   | PRI | NULL    |                |
| unit_price   | decimal(11) | NO   |     | NULL    |                |
+--------------+-------------+------+-----+---------+----------------+
staudenmeir commented 3 years ago

Have you looked into https://github.com/staudenmeir/eloquent-has-many-deep#existing-relationships? It seems like that would be the easiest solution in your case.