Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Laravel-如何处理自定义中间表的加载关系_Php_Laravel_Eloquent_Eager Loading - Fatal编程技术网

Php Laravel-如何处理自定义中间表的加载关系

Php Laravel-如何处理自定义中间表的加载关系,php,laravel,eloquent,eager-loading,Php,Laravel,Eloquent,Eager Loading,我有三个模型,SalesReturn、Product和ProductSalesReturn,它们之间的关系如下: 类SalesReturn扩展了模型 { 公共功能产品 { 返回$this->belongToManyProduct::class ->带时间戳 ->使用ProductSalesReturn::类; } } 我使用ProductSalesReturn表示中间表,ProductSalesReturn与单位有关系: 类ProductSalesReturn扩展了Pivot { 公共职能股 {

我有三个模型,SalesReturn、Product和ProductSalesReturn,它们之间的关系如下:

类SalesReturn扩展了模型 { 公共功能产品 { 返回$this->belongToManyProduct::class ->带时间戳 ->使用ProductSalesReturn::类; } } 我使用ProductSalesReturn表示中间表,ProductSalesReturn与单位有关系:

类ProductSalesReturn扩展了Pivot { 公共职能股 { 返回$this->belongsToUnit::class; } } 当我加载单元关系时,如以下代码所示:

SalesReturn::使用['products'、'products.unit']->查找$id; 我将得到以下错误:

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'units.product_id' in 'where clause' (SQL: select * from `units` where `units`.`product_id` in (1031, 1631, 13391, 14361, 16981, 17441, 41982, 45982, 55741) and `units`.`deleted_at` is null)
表架构如下所示:

创建表“销售退货” `id`bigint20无符号非空自动增量, `编号'varchar20 COLLATE utf8mb4\u unicode\u ci非空, `用户_id`bigint20不为空, `订单号'bigint20非空, `注意`文本整理utf8mb4\u unicode\u ci不为空, `在“timestamp NULL DEFAULT NULL”处创建了_, `在`时间戳为空默认为空'处更新了_, 主键'id` ENGINE=InnoDB AUTO_INCREMENT=7默认字符集=utf8mb4 COLLATE=utf8mb4_unicode_ci; 创建表“产品销售退货” `id`bigint20无符号非空自动增量, `销售退货标识'bigint20不为空, `product_id`bigint20不为空, `单元id`bigint20不为空, `price`double不能为空, `金额'int11不为空, `毛利'double不为空默认值'0', `在“timestamp NULL DEFAULT NULL”处创建了_, `在`时间戳为空默认为空'处更新了_, 主键'id` ENGINE=InnoDB AUTO_INCREMENT=17默认字符集=utf8mb4 COLLATE=utf8mb4_unicode_ci; 如何在Laravel中加载自定义中间表的关系

谢谢。

您可以使用

装置

配置

用法

在Products-BelongTomany关系中定义表、foreignPivotKey和relatedPivotKey参数

composer require ajcastro/eager-load-pivot-relations
use AjCastro\EagerLoadPivotRelations\EagerLoadPivotTrait;
class Product extends Model
{
    // Use the trait here to override eloquent builder.
    // It is used in this model because it is the relation model defined in
    // SalesReturn::products() relation.
    use EagerLoadPivotTrait;
}
return SalesReturn::with('products.pivot.unit')->get();