如何从Laravel的hasMany()关系中获取所有结果? [英] How do I get all of the results from a hasMany() relationship in Laravel?

查看:2579
本文介绍了如何从Laravel的hasMany()关系中获取所有结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,我有一个产品,我有一个BaseProduct。

For example, I have a Product, and I have a BaseProduct.

在产品型号中,我指定了以下内容:

In the model for the Product, I've specified the following:

//In class Product
public function BaseProduct()
{
    return $this->belongsTo("BaseProduct", "BaseProductId");
}

在BaseProduct中,我指定了以下关系:

In the BaseProduct, I've specified the following relationship:

//In class BaseProduct
public function Products()
{
    return $this->hasMany("Product", "ProductId");
}

如果我选择一个产品,像这样:

If I were to select a product, like so:

$Product::first()

我可以通过执行以下操作获得BaseProduct:

I could get the BaseProduct by doing the following:

$Product::first()->BaseProduct()->get();

而不是获取结果的数组,我如何获得 BaseProduct的模型,所以我可以得到BaseProduct的所有子项,这意味着所有具有与此BaseProduct相关的外键的产品。

Instead of getting the array of the result from that, how would I get the Model of the BaseProduct, so I can get all of the children of BaseProduct, meaning all Products that have a foreign key relating to this BaseProduct.

我尝试过 BaseProduct() - > all(); ,但这不是一个有效的方法。

I've tried BaseProduct()->all(); instead, but it isn't a valid method.

编辑:

我已经创建了以下的函数调用链,但很糟糕。 p>

I've created the following chain of function calls - but it's awful.

return BaseProduct::find(Product::first()->BaseProduct()->getResults()['BaseProductId'])->Products()->getResults();






最终编辑:


Final edit:

我在我的 BaseProduct 模型中犯了一个错误。在 Products()函数中,我已经指定了 return $ this-> hasMany(Product,ProductId); 其中 ProductId 应该是 BaseProductId

I had made a mistake in my BaseProduct model. In the Products() function, I had specified return $this->hasMany("Product", "ProductId"); where ProductId should have been BaseProductId.

修正后,我可以成功地使用:

After I fixed that, I could successfully use:

Product::first()->BaseProduct->products;

正如Sheikh Heera所解释的那样。

As Sheikh Heera had explained.

推荐答案

要获得 BaseProduct 的孩子,您可以尝试以下方式:

To get the children of the BaseProduct you may try this:

$bp = BaseProduct::with('Products')->get();

现在,您收集了一个 BaseProduct 所以,你可以使用这样的东西:

Now, you have a collection of BaseProduct so, you may use something like this:

$bp->first()->products

或收集第二个项目

$bp->get(1)->products

另外,你可能会运行这样的循环(最可能在通过后的视图中):

Also, you may run a loop like this (most probably in the view after pass it):

// From the controller
$bp = BaseProduct::with('Products')->get();
return View::make('view_name')->with('baseProduct', $bp);

查看

@foreach($baseProduct->products as $product)
    {{ $product->field_name }}
@endforeach

更新:是的,你可以尝试这个

Update: Yes, you may try this

$product = Product::first();
$baseProduct = $product->BaseProduct;

// Dump all children/products of this BaseProduct
dd($baseProduct->products->toArray());

您可能链接如下:

Product::first()->BaseProduct->products;

更新:您的表格结构应如下所示:

Update: Your table structure should look something like:

表:baseproduct

id(pk) | some_field | another_field

表:产品

id(pk) | baseproduct_id(fk) | another_field

根据这个表格结构,关系应该是

According to this table structure, relationship should be

// BaseProduct
public function Products()
{
    return $this->hasMany("Product");
}

// Product
public function Products()
{
    // second parameter/baseproduct_id is optional unless
    // you have used something else than baseproduct_id
    return $this->belongsTo("BaseProduct", "baseproduct_id");
}

这篇关于如何从Laravel的hasMany()关系中获取所有结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆