Laravel通过其他具有表名而不是id的表进行口对多语,并获取其他数据

洛伦森

我有两个主表,“帖子”和“类别”,应该具有多对多关系。我知道您可以使用名为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'}}
]

我希望这不会太令人困惑,并且有人可以帮助我!

Qonvex620

我认为您的第二个查询已经解决了您的问题。只是像这样做一些修改。

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] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

SQL连接表以获取其他表数据

来自分类Dev

从其他表中获取其他字段

来自分类Dev

Laravel-从数据透视表中获取其他列值

来自分类Dev

两个表获取其他表中具有共同值或没有值的值

来自分类Dev

Linq:获取其他表中存在值的行

来自分类Dev

SQL max函数获取其他表

来自分类Dev

mysql需要获取其他表的依赖计数

来自分类Dev

如何通过小村庄中的外键获取其他表的价值?

来自分类Dev

使用ID从其他表中获取数据

来自分类Dev

如何从触发器内部获取其他表中的插入ID?

来自分类Dev

一个表可以获取其他表列数据吗

来自分类Dev

SQL从其他表获取ID的名称

来自分类Dev

如何从其他表中获取数据?

来自分类Dev

作为其他查询的结果,从带有表名的 select 语句中获取数据

来自分类Dev

从其他具有laravel中主表ID的表创建查询

来自分类Dev

Laravel在知道ID的情况下从视图中的其他表获取数据

来自分类Dev

Laravel表之间通过其他表的雄辩关系

来自分类Dev

MySql:通过从其他表的父ID获取一个表的总和

来自分类Dev

如何从其他数据表创建具有列结构的新数据表?

来自分类Dev

如何从其他数据表创建具有列结构的新数据表?

来自分类Dev

获取其他空行

来自分类Dev

JOIN 表以从其他表中获取 ID 名称

来自分类Dev

如何从具有其他表条件的表中选择

来自分类Dev

从其他表没有的表中获取结果

来自分类Dev

从其他表没有的表中获取结果

来自分类Dev

使用具有多个条件的其他表中的数据更新表?

来自分类Dev

数据库:什么设计具有引用其他表的“文档表”

来自分类Dev

使用其他表的列数据创建具有列名的表

来自分类Dev

SSIS-当其他表包含具有特定值的数据时截断表

Related 相关文章

热门标签

归档