Laravel关联预加载:调用未定义的函数productListing()。

huangapple go评论104阅读模式
英文:

Laravel relationships eager loading: Call to undefined function productListing()

问题

我有一系列相关的模型,我想一次性使用Eloquent的急加载功能加载它们,参考https://laravel.com/docs/10.x/eloquent-relationships#nested-eager-loading。在我的情况下,我正在尝试执行以下操作:

  1. $order = Orders::where('id', $orderId)
  2. ->with([
  3. "users",
  4. "shippingInvoices" => [
  5. "shippingInvoiceDetails" => [
  6. "orderDetails" => "productListing",
  7. ],
  8. "vendor"
  9. ]
  10. ])->get();

但我遇到了以下错误:调用未定义的函数productListing()。

在Orders模型定义文件Orders.php中,我有:

  1. public function users() {
  2. return $this->belongsTo('App\Users', 'user_id');
  3. }
  4. public function shippingInvoices() {
  5. return $this->hasMany(ShippingInvoice::class, "order_id");
  6. }

在ShippingInvoice.php中,我有:

  1. public function shippingInvoiceDetails(): HasMany
  2. {
  3. return $this->hasMany(ShippingInvoiceDetails::class, "invoice_id");
  4. }

在ShippingInvoiceDetails.php中:

  1. public function orderDetails(): BelongsTo
  2. {
  3. return $this->belongsTo(OrderDetails::class);
  4. }

在OrderDetials.php中:

  1. public function productListing() {
  2. return $this->belongsTo('App\ProductListing', 'product_id');
  3. }
英文:

I have a chain of related models that I want to load up together at once using Eloquent's eager loading feature, as per https://laravel.com/docs/10.x/eloquent-relationships#nested-eager-loading. In my own case, I am trying to do:

  1. $order = Orders::where('id', $orderId)
  2. ->with([
  3. "users",
  4. "shippingInvoices" => [
  5. "shippingInvoiceDetails" => [
  6. "orderDetails" => "productListing",
  7. ],
  8. "vendor"]
  9. ])->get();

But I get the following error: Call to undefined function productListing().<br>
In the Orders model definition file, Orders.php, I have:

  1. public function users() {
  2. return $this-&gt;belongsTo(&#39;App\Users&#39;, &#39;user_id&#39;);
  3. }
  4. public function shippingInvoices() {
  5. return $this-&gt;hasMany(ShippingInvoice::class, &quot;order_id&quot;);
  6. }

In ShippingInvoice.php, I have:

  1. public function shippingInvoiceDetails(): HasMany
  2. {
  3. return $this-&gt;hasMany(ShippingInvoiceDetails::class, &quot;invoice_id&quot;);
  4. }

In ShippingInvoiceDetails.php:

  1. public function orderDetails(): BelongsTo
  2. {
  3. return $this-&gt;belongsTo(OrderDetails::class);
  4. }

In OrderDetials.php:

  1. public function productListing() {
  2. return $this-&gt;belongsTo(&#39;App\ProductListing&#39;, &#39;product_id&#39;);
  3. }

答案1

得分: 1

你需要按照以下方式调用它:

  1. $order = Orders::where('id', $orderId)
  2. ->with([
  3. "users",
  4. "shippingInvoices",
  5. "shippingInvoices.shippingInvoiceDetails",
  6. "shippingInvoices.shippingInvoiceDetails.orderDetails",
  7. "shippingInvoices.shippingInvoiceDetails.orderDetails.productListing",
  8. "vendor"
  9. ])->get();

你可以使用点号(.)来访问相关的模型。

英文:

You have to call it like bellow.

  1. $order = Orders::where(&#39;id&#39;, $orderId)
  2. -&gt;with([
  3. &quot;users&quot;,
  4. &quot;shippingInvoices&quot;,
  5. &quot;shippingInvoices.shippingInvoiceDetails&quot;,
  6. &quot;shippingInvoices.shippingInvoiceDetails.orderDetails&quot;,
  7. &quot;shippingInvoices.shippingInvoiceDetails.orderDetails.productListing&quot;,
  8. &quot;vendor&quot;
  9. ])-&gt;get();

you can access the related model with dot (.)

huangapple
  • 本文由 发表于 2023年7月27日 21:03:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/76780026.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定