php 如何从 Laravel 中的 hasMany() 关系中获取所有结果?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21591384/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I get all of the results from a hasMany() relationship in Laravel?
提问by Nathan Lutterman
For example, I have a Product, and I have a BaseProduct.
例如,我有一个产品,我有一个 BaseProduct。
In the model for the Product, I've specified the following:
在产品的模型中,我指定了以下内容:
//In class Product
public function BaseProduct()
{
return $this->belongsTo("BaseProduct", "BaseProductId");
}
In the BaseProduct, I've specified the following relationship:
在 BaseProduct 中,我指定了以下关系:
//In class BaseProduct
public function Products()
{
return $this->hasMany("Product", "ProductId");
}
If I were to select a product, like so:
如果我要选择一个产品,像这样:
$Product::first()
I could get the BaseProduct by doing the following:
我可以通过执行以下操作来获取 BaseProduct:
$Product::first()->BaseProduct()->get();
Instead of getting the array of the result from that, how would I get the Modelof the BaseProduct, so I can get all of the children of BaseProduct, meaning all Products that have a foreign key relating to this BaseProduct.
而不是从中获取结果数组,我将如何获取ModelBaseProduct 的数组,这样我就可以获得 BaseProduct 的所有子项,这意味着所有具有与此 BaseProduct 相关的外键的产品。
I've tried BaseProduct()->all();instead, but it isn't a valid method.
我已经尝试过了BaseProduct()->all();,但这不是一个有效的方法。
Edit:
编辑:
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:
最终编辑:
I had made a mistake in my BaseProductmodel. In the Products()function, I had specified return $this->hasMany("Product", "ProductId");where ProductIdshould have been BaseProductId.
我在我的BaseProduct模型中犯了一个错误。在Products()函数中,我已经指定return $this->hasMany("Product", "ProductId");了ProductId应该在哪里BaseProductId。
After I fixed that, I could successfully use:
修复后,我可以成功使用:
Product::first()->BaseProduct->products;
As Sheikh Heera had explained.
正如谢赫·希拉 (Sheikh Heera) 所解释的那样。
回答by The Alpha
To get the children of the BaseProductyou may try this:
为了得到BaseProduct你的孩子,你可以试试这个:
$bp = BaseProduct::with('Products')->get();
Now, you have a collection of BaseProductso, you may use something like this:
现在,你有一个BaseProductso的集合,你可以使用这样的东西:
$bp->first()->products
Or get the second item from collection
或者从集合中获取第二个项目
$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);
In the View
在里面 View
@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());
You may chain like:
你可以像这样链接:
Product::first()->BaseProduct->products;
Update:Your table structure should look something like:
更新:您的表结构应如下所示:
Table:baseproduct:
表:基本产品:
id(pk) | some_field | another_field
Table:products:
表:产品:
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");
}
回答by user1848769
$product = Product::find('id');
$baseProduct = $product->baseProduct()->getModel();
$baseProducts->products()->getModels();

