Php 拉威尔关系
我正在创建一个基本的论坛,只是为了在学习Laravel的同时能做些有意义的事情。 所以就像每个论坛都是有组织的一样,在主页上我希望有一个类别及其子类别的列表,每个子类别中的帖子总数,同时还有一个到最新帖子的链接 因此,这种关系有很多:Php 拉威尔关系,php,laravel,relationship,Php,Laravel,Relationship,我正在创建一个基本的论坛,只是为了在学习Laravel的同时能做些有意义的事情。 所以就像每个论坛都是有组织的一样,在主页上我希望有一个类别及其子类别的列表,每个子类别中的帖子总数,同时还有一个到最新帖子的链接 因此,这种关系有很多: Category -> has Subcategories -> has Threads -> has Posts. 在控制器方法中,我有 $cat = Category::with('subcategory')->get(); retu
Category -> has Subcategories -> has Threads -> has Posts.
在控制器方法中,我有
$cat = Category::with('subcategory')->get();
return View::make('forum.index', compact('cat'));
这适用于类别和子类别的基本列表,但我不能理解其余的。
这肯定不行
Category::with('subcategory')->with('threads')->with('posts')->get();
$data = Category::with('subcategories.threads')->with(array('posts' => function($query)
{
$query->take(1);
}))->get();
因为它们之间的关系是不确定的。从拉威尔的文件来看,有许多粗略的关系。这是解决办法吗
class Category extends Eloquent {
public function subcategory() {
return $this->hasMany('Subcategory');
}
public function posts() { // not sure about this cause it doesnt work
return $this->hasManyThrough('Post', 'Thread');
}
}
除此之外,如何为每个子类别获取posts->count()?可以把它分开吗?链接可能会变得复杂
编辑
表列为
类别
id | title
子类别
id | title | category_id
线程
id | title | subcategory_id | user_id
职位
id | title | body | thread_id | user_id
编辑2
仅获取最新帖子的代码是什么?这不管用
Category::with('subcategory')->with('threads')->with('posts')->get();
$data = Category::with('subcategories.threads')->with(array('posts' => function($query)
{
$query->take(1);
}))->get();
您只设置了一个有效的关系,即:
class Category extends Eloquent {
public function subcategory() {
return $this->hasMany('Subcategory');
}
}
在其他模型中声明其他关系:
class Subcategory extends Eloquent {
public function threads() {
return $this->hasMany('Thread');
}
}
class Thread extends Eloquent {
public function posts() {
return $this->hasMany('Post');
}
}
声明关系后,您可以使用:
$categories = Category::with('subcategory.threads.posts')->get();
class Category extends Eloquent {
public function subcategories() {
return $this->hasMany('Subcategory');
}
}
由于一个类别
有许多子类别
,因此在类别
模型中使用子类别
的复数名称,而不是子类别
,因此,例如,您可以使用:
$categories = Category::with('subcategory.threads.posts')->get();
class Category extends Eloquent {
public function subcategories() {
return $this->hasMany('Subcategory');
}
}
然后还:
$categories = Category::with('subcategories.threads.posts')->get();
所有关系都将作为嵌套对象集合进行检索。例如:
$categories->subcategories; // Will be a collection of Subcategory models
$categories->subcategories->threads // Will be a collection of Thread models
$categories->subcategories->threads->posts // Will be a collection of Post models
您可以通过以下方式声明hasManyThrough
子类别和Post
之间的关系:
class Subcategory extends Eloquent {
public function threads() {
return $this->hasMany('Thread');
}
public function posts() {
return $this->hasManyThrough('Post', 'Thread');
}
}
此外,您还可以通过
子类别
在类别
和线程
之间建立关系。您只设置了一个有效的关系,即:
class Category extends Eloquent {
public function subcategory() {
return $this->hasMany('Subcategory');
}
}
在其他模型中声明其他关系:
class Subcategory extends Eloquent {
public function threads() {
return $this->hasMany('Thread');
}
}
class Thread extends Eloquent {
public function posts() {
return $this->hasMany('Post');
}
}
声明关系后,您可以使用:
$categories = Category::with('subcategory.threads.posts')->get();
class Category extends Eloquent {
public function subcategories() {
return $this->hasMany('Subcategory');
}
}
由于一个类别
有许多子类别
,因此在类别
模型中使用子类别
的复数名称,而不是子类别
,因此,例如,您可以使用:
$categories = Category::with('subcategory.threads.posts')->get();
class Category extends Eloquent {
public function subcategories() {
return $this->hasMany('Subcategory');
}
}
然后还:
$categories = Category::with('subcategories.threads.posts')->get();
所有关系都将作为嵌套对象集合进行检索。例如:
$categories->subcategories; // Will be a collection of Subcategory models
$categories->subcategories->threads // Will be a collection of Thread models
$categories->subcategories->threads->posts // Will be a collection of Post models
您可以通过以下方式声明hasManyThrough
子类别和Post
之间的关系:
class Subcategory extends Eloquent {
public function threads() {
return $this->hasMany('Thread');
}
public function posts() {
return $this->hasManyThrough('Post', 'Thread');
}
}
此外,您还可以通过子类别
在类别
和线程
之间建立关系
问题类别和问题['category\u id']
和项目问题['question\u id']
这段代码已经为我工作了,希望对你有用
$categories=ExamCategory::with(['question' => function ($query) use($exam){$query->where('exam_id','=',$exam->id)->with('Items');}])->get();
我有
问题类别和问题['category\u id']
和项目问题['question\u id']
这段代码已经为我工作了,希望对你有用
$categories=ExamCategory::with(['question' => function ($query) use($exam){$query->where('exam_id','=',$exam->id)->with('Items');}])->get();
请提供表中使用的字段名。@Wewerwolf TheAlpha我已经编辑了我的帖子。我将感谢任何帮助:)看我的答案,可能会有帮助;)请提供表中使用的字段名。@Wewerwolf TheAlpha我已经编辑了我的帖子。我将感谢任何帮助:)看我的答案,可能会有帮助;)我在模型中设置了这些关系,但是用箭头链接不起作用,而用圆点链接效果很好。我只担心获取所有内容,因为我并不想获取所有的线程和帖子,我只需要数一数。但我如何只加载最新的帖子呢?我已经编辑了我的问题。我试着用laravel docs的例子来扩展你的答案。。。感谢您的时间:)使用
$yourModel->orderBy('id','desc')->获取(1)
我尝试过,但错误是“调用未定义的方法illumb\Database\Query\Builder::posts()”。我想我需要一个分类模型中的“posts”方法?但是,我在模型中设置了这些关系,但是用箭头链接不起作用,而用圆点链接效果很好。我只担心获取所有内容,因为我并不想获取所有的线程和帖子,我只需要数一数。但我如何只加载最新的帖子呢?我已经编辑了我的问题。我试着用laravel docs的例子来扩展你的答案。。。感谢您的时间:)使用$yourModel->orderBy('id','desc')->获取(1)
我尝试过,但错误是“调用未定义的方法illumb\Database\Query\Builder::posts()”。我想我需要一个分类模型中的“posts”方法?但是,如何回到那里呢?