我在eager load-php laravel中遇到问题

gpnt7bae  于 2021-06-21  发布在  Mysql
关注(0)|答案(0)|浏览(230)

在从我的模型i eager load 3关系中获取数据时,我遇到了一个关于eager load的问题,要理解这里的问题是从我的模型i eager load 3关系中获取数据的迁移

$table->increments('id'); $table->unsignedInteger('order_id'); $table->unsignedInteger('product_item_id'); $table->unsignedInteger('product_id'); $table->integer('days'); $table->foreign('order_id')

        ->references('id')

        ->on('orders')

        ->onDelete('cascade');
    $table->foreign('product_item_id')

        ->references('id')

        ->on('product_items')

        ->onDelete('cascade');
    $table->foreign('product_id')

        ->references('id')

        ->on('products')

        ->onDelete('cascade');
    $table->timestamps();

这里的模型包含了关系和急负荷属性

protected $with = ['order', 'item', 'product'];

public function item()
{
    return $this->hasOne(ProductItem::class, 'id');
}

public function product()
{
    return $this->hasOne(Product::class, 'id');
}

public function order()
{
    return $this->hasOne(Order::class, 'id');
}

当我试图从这个模型中获取数据时,得到的回答是“我将忽略对象中不重要的属性”

Collection {#1478 ▼
  #items: array:2 [▼
    0 => OrderItem {#1071 ▼
      #guarded: []
      #with: array:3 [▼
        0 => "order"
        1 => "item"
        2 => "product"
      ]
      -amount: null
      #connection: "mysql"
      #table: null
      #primaryKey: "id"
      #keyType: "int"
      +incrementing: true
      #withCount: []
      #perPage: 15
      +exists: true
      +wasRecentlyCreated: false
      #attributes: array:7 [▼
        "id" => 1
        "order_id" => 1
        "product_item_id" => 1
        "product_id" => 1
        "days" => 0
        "created_at" => "2018-07-10 08:15:02"
        "updated_at" => "2018-07-10 08:15:02"
      ]
      #original: array:7 [▶]
      #changes: []
      #casts: []
      #dates: []
      #dateFormat: null
      #appends: []
      #dispatchesEvents: []
      #observables: []
      #relations: array:3 [▼
        "order" => Order {#1166 ▶}
        "item" => ProductItem {#1265 ▶}
        "product" => Product {#1366 ▶}
      ]
      #touches: []
      +timestamps: true
      #hidden: []
      #visible: []
      #fillable: []
    }
    1 => OrderItem {#1072 ▼
      #guarded: []
      #with: array:3 [▼
        0 => "order"
        1 => "item"
        2 => "product"
      ]
      #connection: "mysql"
      #attributes: array:7 [▼
        "id" => 21
        "order_id" => 1
        "product_item_id" => 15
        "product_id" => 4
        "days" => 0
        "created_at" => "2018-07-10 12:44:32"
        "updated_at" => "2018-07-10 12:44:32"
      ]
      #original: array:7 [▶]
      #relations: array:3 [▼
        "order" => null
        "item" => null
        "product" => null
      ]
    }
  ]
}

我确信我的外键值是100%正确分配的
问题是。。在响应的第一个对象中,我得到了正确的渴望加载,但第二个对象返回null!!有产品id为4,产品\u项id为15!!有人能帮忙知道发生了什么吗?

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题