Laravel 从 ID 中获取模型,使用 owntoMany
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28708697/
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
Laravel get model from ID with belongtoMany
提问by O?uz ?i?ek
I'm building an application using Laravel 4 but have some problems with the pivot tables.
我正在使用 Laravel 4 构建应用程序,但数据透视表存在一些问题。
there are 3 tables Categories , Products , products_categories (pivot)
有 3 个表 Categories , Products , products_categories (pivot)
Category model
品类模型
public function product()
{
return $this->belongsToMany('Product', 'products_categories');
}
Products Model
产品型号
public function category()
{
return $this->belongsToMany('Category', 'products_categories');
}
products_categories
table has product_id
and category_id
columns.
products_categories
表有product_id
和category_id
列。
What I want is take all products in this category and list them in views
我想要的是获取此类别中的所有产品并在视图中列出它们
$category = Category::where('id' , '=' , '7')->first();
foreach($category->product as $product){
echo $product->id;
}
I can see product ids related with particular category but when I want to use it to get all product itself like:
我可以看到与特定类别相关的产品 ID,但是当我想使用它来获取所有产品本身时,例如:
$category = Category::where('id' , '=' , '7')->first();
foreach($category->product as $product){
$product = Product::where('id' , '=' , $product->id )->get();
}
return View::make('index')->with('product',$product);
it doesn't work :( with this error
它不起作用:(出现此错误
Trying to get property of non-object
试图获取非对象的属性
I tried this
我试过这个
$category = Category::where('id' , '=' , '7')->first();
$product = array();
foreach($category->product as $product){
$product[] = Product::where('id' , '=' , $product->id )->get();
}
return View::make('index')->with('product',$product);
this time it throws this error
这次它抛出这个错误
Missing argument 2 for Illuminate\Database\Eloquent\Model::setAttribute()
Illuminate\Database\Eloquent\Model::setAttribute() 缺少参数 2
How can I solve this?
我该如何解决这个问题?
回答by patricus
The immediate issue is that you're trying to reuse your iterator variable from the foreach loop. This is leading to your unexpected results.
当前的问题是您试图从 foreach 循环中重用迭代器变量。这会导致您意想不到的结果。
foreach($category->product as $product) {
^^^^^^^^
$product = Product::where('id' , '=' , $product->id )->get();
^^^^^^^^
}
However, there is no need to do any of that. $category->product
is already a Collection of the Eloquent Product models. There is no need to try and retrieve the individual products again; you already have them.
但是,没有必要执行任何操作。$category->product
已经是 Eloquent 产品模型的集合。无需再次尝试检索单个产品;你已经拥有了。
If you're trying to pass this Collection to the view, you could just do:
如果您尝试将此 Collection 传递给视图,则可以执行以下操作:
return View::make('index')->with('product', $category->product);
Also, as a side note, if you're trying to find a record by the id, you can use the find()
method:
另外,作为旁注,如果您尝试通过 id 查找记录,则可以使用以下find()
方法:
$category = Category::find(7);