我有点担心拉拉维尔模型的循环问题。我有三个相互关联的表,其中一个应该被孩子圈起来,并显示其他表的关系,那么这里的交易是什么?
我有三张桌子:
categories
- id
- parent\_id = relation to the same table foreign id
- name = string
threads
- id
- category\_id = relation with categories
- title
- desc
posts
- id
- category\_id = to make query easier
- thread\_id = relation w
我有以下实体:
public class Category
{
public int ID { get; set; }
public int? ParentID { get; set; }
public string Name{ get; set; }
public virtual Category Parent { get; set; }
}
我正在尝试定义一个关系:Category可以有一个父类别。
我已经定义了主键:
HasKey(m => m.ID);
我读过示例,其中包含一对零或一对一的关系,但我不能准确地理解HasRequired是如何暗示实体
假设我有parent->child one下的产品和parent->child one->child one child下的另一个产品,我所能得到的只有最后一个类别名称,比如:child one和child one child,但是前面的父级名称我无法得到。
代码
类别模型:
public function categories()
{
return $this->hasMany(Category::class);
}
public function childs() {
return $this->hasMany(Category::
我试图使用Laravel 5的(L5)模型::with ()方法,获得具有给定类别的产品的简单列表。但是L5似乎忽略了where子句的类别。
我的产品模型中的关系:
public function categories(){
return $this->belongsToMany('App\Category', 'categories_products');
}
在我的主计长中:
public function getByCategory($slug){
$return = Product::with(array('categori