我有两个主表,“帖子”和“类别”,应该具有多对多关系。我知道您可以使用名为category_post的表来完成此操作,但这不是我可以使用的结构的设置方式。
就像这样:帖子与问题具有一对一的关系,并且问题在帖子中具有类别。
我的数据库如下所示:
Schema::create('posts', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('title');
$table->text('contet');
}
Schema::create('post_questions', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('post_id');
$table->integer('views');
$table->integer('answers');
}
Schema::create('categories', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name')->unique();
}
Schema::create('post_question_categories', function (Blueprint $table) {
$table->bigIncrements('id');
$table->integer('post_question_id');
$table->string('category_name');
$table->string('additional_info');
}
如您所见,我的数据透视表既没有post_id也没有category_id,而是没有post_question_id和category_name,在category表中是唯一的。但是应有状态是具有职位和类别的多对多状态。
问题也具有一对多关系来查询Additional_infos
这是我的模型:
class Post extends Model
{
public function question()
{
return $this->hasOne('App\PostQuestion')->with('categories');
}
}
class Question extends Model
{
public function post()
{
return $this->belongsTo('App\Post');
}
public function categories()
{
return $this->hasMany('App\PostQuestionCategory');
}
}
class PostQuestionCategory extends Model
{
public function question()
{
return $this->belongsTo('App\PostQuestion')->with('post');
}
}
class Category extends Model
{
public function posts()
{
}
}
这就是乐趣的开始:我想查询一个类别的所有帖子。我已经尝试过,并通过3种方法获得了此结果:
1。
public function posts()
{
$name = $this->name;
return Post::whereHas('question', function ($question) use ($name) {
$question->whereHas('categories', function ($categories) use ($name) {
$categories->where('name', $name);
});
})->get();
}
2。
public function posts()
{
return PostQuestionCategory::with('question')->where('name', $this->name)->get();
}
3.将post_id和category_id添加到表中
public function posts()
{
return $this->belongsToMany('App\Post', 'post_question_categories');
}
第三个不是真正的选择,因为我不想操纵现有的数据库。在第二个中,我并没有真正将post对象作为一个关系,而只是作为一个简单的查询结果。
当我在category / show.blade.php中做
@foreach ($category->posts() as $post)
{{ $post->question->categories }}
@endforeach
因此,我无法访问许多属性。
所以我更喜欢使用第一个选项。
事情是:
我只想获取当前正在使用的类别。通过该查询,我的确会得到所有具有该查询的帖子,但是在foreach中,我还将获得所有其他类别。此外,我想在此实例中获取Additional_info属性。
像这样:我在路线类别上。show/ 1(名称= cat1)
我的数据库就像
帖子
id title
1 a
2 b
3 c
4 d
post_questions
id post_id views answers
1 1 12 2
2 2 7 0
3 3 456 6
4 4 124 11
post_question_categories:
id question_id additional_info category_name
1 1 ai1 cat 1
2 1 ai2 cat 2
3 3 ai1 cat 1
4 4 ai1 cat 3
分类
id name
1 cat 1
2 cat 2
3 cat 3
我理想的输出是:
posts
[
{id: 1, title: 'a', question: {id: 1, views: 12, answers: 2, categories: [{id: 1, additional_info: 'ai1', name: 'cat1'}, // Not id 2 since im on show/1]},
{id: 3, title: 'c', question: {id: 3, views: 456, answers: 6, categories: [{id: 1, additional_info: 'ai1', name: 'cat1'}}
]
我希望这不会太令人困惑,并且有人可以帮助我!
我认为您的第二个查询已经解决了您的问题。只是像这样做一些修改。
public function posts()
{
return PostQuestionCategory::with('question.post')->where('category_name', $this->name)->get();
}
只需将帖子和问题一起添加即可将其包含在返回的集合中。
或者,如果您也与类别有关系,则只需添加类别关系,就像这样
public function posts()
{
return PostQuestionCategory::with('question.post', 'category')->where('category_name', $this->name)->get();
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句