pivot表中多个3模型的laravel雄辩关系问题
我有一个有好几种型号的箱子。订单模型、产品模型、港口模型和物流模型。 如果pivot表中多个3模型的laravel雄辩关系问题,laravel,eloquent,many-to-many,eager-loading,Laravel,Eloquent,Many To Many,Eager Loading,我有一个有好几种型号的箱子。订单模型、产品模型、港口模型和物流模型。 如果订单属于许多产品,订单属于具有“收件人端口id”的端口,订单属于具有“物流id”的物流,产品属于具有“发件人端口id”的端口 从港口id到港口id、物流id和单价与数据透视表结合使用 现在我想问一下。如何从\u port\u id、to\u port\u id、logistic\u id检索具有相应的不同产品的订单,以尽可能少的sql获得不同的单价 订单模式 +--------------+-------------+--
订单属于许多产品
,订单属于具有“收件人端口id”的端口
,订单属于具有“物流id”的物流
,产品属于具有“发件人端口id”的端口
从港口id到港口id、物流id和单价
与数据透视表结合使用
现在我想问一下。如何从\u port\u id、to\u port\u id、logistic\u id检索具有相应的不同产品的订单,以尽可能少的sql获得不同的单价
订单模式
+--------------+-------------+------+-----+---------+----------------+
| 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 | |
+--------------+-------------+------+-----+---------+----------------+
+--------------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------+----------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| from_port_id | int(11) | NO | | NULL | |
+--------------+-------------+------+-----+---------+----------------+
+--------------+-------------+------+-----+---------+----------------+
| 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 | |
+--------------+-------------+------+-----+---------+----------------+
产品型号
+--------------+-------------+------+-----+---------+----------------+
| 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 | |
+--------------+-------------+------+-----+---------+----------------+
+--------------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------+----------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| from_port_id | int(11) | NO | | NULL | |
+--------------+-------------+------+-----+---------+----------------+
+--------------+-------------+------+-----+---------+----------------+
| 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 | |
+--------------+-------------+------+-----+---------+----------------+
物流港数据透视模型
+--------------+-------------+------+-----+---------+----------------+
| 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 | |
+--------------+-------------+------+-----+---------+----------------+
+--------------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------+----------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| from_port_id | int(11) | NO | | NULL | |
+--------------+-------------+------+-----+---------+----------------+
+--------------+-------------+------+-----+---------+----------------+
| 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 | |
+--------------+-------------+------+-----+---------+----------------+
----------更新-------------------------
class Product extends Model
{
protected $table = 'product';
/*
|--------------------------------------------------------------------------
| RELATIONS
|--------------------------------------------------------------------------
*/
public function orderTransact(){
return $this->belongsToMany('App\Models\OrderTransact','order_transact_product','product_id','order_transact_id')->withPivot(['unique_order_product_id','order_quantity_in_log','order_product_status_id ','confirmed_delivery_date','expected_ETD','due_at','downpayment','deposit','total','feedback','created_at','updated_at','tracking_no','weight','logistic_cost'])->using('App\Models\OrderTransactProduct')->as('order_product');
}
public function orderProduct(){
return $this->hasMany('App\Models\OrderTransactProduct','product_id','id');
}
public function fromLogisticPort(){
return $this->belongsTo('App\Models\LogisticPort','from_port_id','id');
}
}
class LogisticPort extends Model
{
use HasFactory;
protected $table = 'logistic_port';
public function logisticFrom(){
return $this->belongsToMany('App\Models\Logistic','logistic_port_logistic','from_port_id','logistic_id')->withPivot(['to_port_id','courier_charges_unit'])->using('App\Models\LogisticPortLogistic');
}
public function logisticTo()
{
return $this->belongsToMany('App\Models\Logistic','logistic_port_logistic','to_port_id','logistic_id')->withPivot(['from_port_id','courier_charges_unit'])->using('App\Models\LogisticPortLogistic');
}
public function logisticPortFrom(){
return $this->belongsToMany('App\Models\LogisticPort','logistic_port_logistic','to_port_id','from_port_id')->withPivot(['logistic_id','courier_charges_unit'])->using('App\Models\LogisticPortLogistic');
}
public function logisticPortTo(){
return $this->belongsToMany('App\Models\LogisticPort','logistic_port_logistic','from_port_id','to_port_id')->withPivot(['logistic_id','courier_charges_unit'])->using('App\Models\LogisticPortLogistic');
}
public function toPivotLogisticPort(){
return $this->hasMany('App\Models\LogisticPortLogistic','to_port_id','id');
}
public function product(){
return $this->hasMany('App\Models\Product','from_port_id','id');
}
public function orderTransact(){
return $this->hasMany('App\Models\OrderTransact','from_port_id','id');
}
}
class Logistic extends Model
{
protected $table = 'logistic';
public function orderTransact(){
return $this->hasMany('App\Models\OrderTransact','order_transact_id','id');
}
public function logisticPortFrom(){
return $this->belongsToMany('App\Models\LogisticPort','logistic_port_logistic','logistic_id','from_port_id')->withPivot(['to_port_id','courier_charges_unit'])->using('App\Models\LogisticPortLogistic');
}
public function logisticPortTo(){
return $this->belongsToMany('App\Models\LogisticPort','logistic_port_logistic','logistic_id','to_port_id')->withPivot(['from_port_id','courier_charges_unit'])->using('App\Models\LogisticPortLogistic');
}
}
class OrderTransact extends Model
{
use HasFactory;
use SoftDeletes;
protected $table = 'order_transact';
/*
|--------------------------------------------------------------------------
| RELATIONS
|--------------------------------------------------------------------------
*/
public function logistic(){
return $this->belongsTo('App\Models\Logistic','logistic_id','id');
}
public function product(){
return $this->belongsToMany('App\Models\Product','order_transact_product','order_transact_id','product_id')->withPivot(['unique_order_product_id','order_quantity_in_log','order_product_status_id','confirmed_delivery_date','expected_ETD','due_at','downpayment','deposit','total','feedback','created_at','updated_at','tracking_no','weight','logistic_cost'])->using('App\Models\OrderTransactProduct')->as('order_product');
}
public function orderProduct(){
return $this->hasMany('App\Models\OrderTransactProduct','order_transact_id','id');
}
public function toLogisticPort(){
return $this->belongsTo('App\Models\LogisticPort','to_port_id','id');
}
}
到目前为止你尝试了什么?我不知道如何更新你的所有关系模型,我不知道你在项目中使用的关系名称,所以很难为你编写代码。。因此,请更新模型,我认为您应该为您的案例应用形态学关系,而不是使用3个表的多对多关系。事实上,对于两个以上模型可以相互关联的场景,引入了形态学关系。