【问题标题】:Laravel relationships functions shows nullLaravel 关系函数显示为空
【发布时间】:2019-11-15 14:53:52
【问题描述】:

我正在创建一个小型网上商店,用户可以在其中创建订单,卖家可以查看订单。我已经定义了关系,但相关函数为空。这就是我的表格在 phpmyAdmin https://imgur.com/a/C2PSmFt

中的样子

这就是我在模型中定义关系的方式。

User.php

public function sellerOrders()
{
    return $this->hasMany(Order::class);
}

Order.php

public function user()
{
    return $this->belongsTo('App\User', 'seller_id');
}

public function products()
{
    return $this->belongsToMany('App\Product')->withPivot('quantity','total','Subtotal');
}

public function productInfo()
 {
     return $this->belongsTo('App\Product');
 }

 public function orderInfo()
 {
    return $this->belongsTo(OrderProduct::class, 'seller_id');
 }

这是我在控制器中的功能

$orders = Auth::user()->sellerOrders()->with('productInfo','orderInfo')->get();

当我dd($orders)时,这就是我得到的

 Collection {#305 ▼
 #items: array:1 [▼
 0 => Order {#286 ▼
  #table: "orders"
  #fillable: array:5 [▶]
  #connection: "mysql"
  #primaryKey: "id"
  #keyType: "int"
  +incrementing: true
  #with: []
  #withCount: []
  #perPage: 15
  +exists: true
  +wasRecentlyCreated: false
  #attributes: array:8 [▼
    "id" => 9
    "user_id" => 1
    "shipping_email" => "878888"
    "shipping_name" => "hattta"
    "shipping_city" => "kljjdjd"
    "shipped" => 0
    "created_at" => "2019-07-05 01:33:58"
    "updated_at" => "2019-07-05 01:33:58"
  ]
  #original: array:8 [▶]
  #changes: []
  #casts: []
  #dates: []
  #dateFormat: null
  #appends: []
  #dispatchesEvents: []
  #observables: []
  #relations: array:2 [▼
    "productInfo" => null
    "orderInfo" => null
  ]
  #touches: []
  +timestamps: true
  #hidden: []
  #visible: []
  #guarded: array:1 [▶]
}
]
}

如何显示productInfoorderInfo?非常感谢您的帮助。

【问题讨论】:

  • 您在user()orderInfo() 中都使用seller_id 作为外键。而且您还没有为productInfo() 放置外键。只是product_info_id吗?
  • 我确实将seller_id 放入productInfo() 并得到null。 @newUserName02
  • seller_id 应该只是其中一个的外键,我猜是用户。您需要为其他 2 填写正确的外键。

标签: php laravel laravel-5 eloquent


【解决方案1】:

如果您查看转储中 Order 模型的属性,则没有字段 seller_id

#attributes: array:8 [▼
"id" => 9
"user_id" => 1                      <--- There is a user_id, but not seller_id
"shipping_email" => "878888"
"shipping_name" => "hattta"
"shipping_city" => "kljjdjd"
"shipped" => 0
"created_at" => "2019-07-05 01:33:58"
"updated_at" => "2019-07-05 01:33:58"
]

您需要在模型中添加seller_id 字段并将值添加到数据库中,或者更简单的方法是更改​​ Order 模型上的关系,以便外键为 @ 987654324@ 而不是 seller_id

修复此问题后,您应该会在订单上看到 **user** 的值。因此,如果您循环访问$orders,您可以执行 $order->user,它应该返回该订单的用户。

您的 productInfo 也是如此。 Order 模型上没有product_id,并且由于非常规名称(即不仅仅是'product()'),您需要在关系方法中指定外键订单模型。另外,不要忘记将 product_id 添加到 Order 模型中作为可填充的。

同样的问题适用于您的 orderInfo() 方法 - 订单模型上没有 seller_id 来提供关系 - 可以应用与上述相同的修复方法。

【讨论】:

  • 我尝试在Order 中添加seller_id,当用户一次从不同的卖家购买多件产品时,订单会转到一个卖家(这意味着卖家收到订单有些产品不是他的产品@Watercayman
  • 我明白了。这是一个不同的问题——你问为什么关系是空的,我已经在上面回答了。您在向卖家混合订单方面提出的问题是一个不同的问题,可能涉及对您的代码进行一些重新思考和重新架构。我建议您首先解决原始问题(使关系正常工作并弄清楚所有使用 FK 的工作原理),然后考虑重新编写代码以解决第二个问题。
【解决方案2】:

因为您有关系 return hasMany => 在订单中您将拥有一个数组,并且由于您已经查询过它,因此您必须像这样查询每个项目:

foreach($orders as $order){
    echo $order->productInfo->propertyOfProductInforTable;
    echo $order->products->name; //or whatever is in your product table relation
}

【讨论】:

  • 如何作为关系查询? @Sarghei
  • @user11710915 您必须确保在您的模型中定义您的关系公共函数,如下所示:public function phone() {return $this-&gt;hasOne('App\Phone', 'foreign_key', 'local_key');},然后您可以查询相关表的属性。
猜你喜欢
  • 1970-01-01
  • 2020-02-06
  • 1970-01-01
  • 2020-11-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-06
  • 2021-03-20
相关资源
最近更新 更多