Laravel.如何在外键是数组的情况下获取关系 [英] Laravel. How to get relationships where foreign key is an array

查看:141
本文介绍了Laravel.如何在外键是数组的情况下获取关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试检索数据库行及其关系.但是,本地键是一个数组.让我用一个例子来解释.

让我们说我有一个国家表和一个页面表.每个国家可以有很多页面.每个页面可以属于多个国家.我没有灵活性来更改此架构.

pages
+-------------+-----------+
| id | name   | countries |
+-------------+-----------+
| 1  | Page 1 | 1         |
+-------------+-----------+
| 2  | Page 2 | 1,2,3     |
+-------------+-----------+
| 3  | Page 3 | 4,5,6     |
+-------------+-----------+

countries
+----+----------------+
| id | name           | 
+----+----------------+
| 1  | United States  |
+----+----------------+
| 2  | United Kingdom |
+----+----------------+
| 3  | Germany        |
+----+----------------+
| 4  | France         |
+----+----------------+
| 5  | Hong Kong      |
+----+----------------+
| 6  | Thailand       |
+----+----------------+
| 7  | Belgium        |
+----+----------------+
| 8  | Singapore      |
+----+----------------+

我的模型和控制器如下所示:

country 

public function pages()
{
    return $this->hasMany(Page::class, 'id', 'countries');
}

MemberController.php

$countries = Country::with('pages')->get();

这将返回所有国家,但只有第1页包含任何关系.

是否可以使用whereIn方法检索关系,以便所有三个国家都能返回适当的页面?

预先感谢

解决方案

由于Page可以属于许多Countries,因此您需要创建一个名为country_page的数据透视表并删除countries列.

然后定义两个 belongsToMany() 关系型号:

public function pages()
{
    return $this->belongsToMany(Page::class);
}

如果您不遵循Laravel命名约定在我的仓库中列出,并且您为数据透视表名称指定了自定义名称,也对其进行了定义:

public function pages()
{
    return $this->belongsToMany(Page::class, 'custom_pivot_table');
}

I am trying to retrieve database rows with their relationships. However, the local key is an array. Let me explain using an example.

Lets say I have a table of countries and a table of pages. Each country can have many pages. Each page can belong to multiple countries. I do not have the flexibility to change this schema.

pages
+-------------+-----------+
| id | name   | countries |
+-------------+-----------+
| 1  | Page 1 | 1         |
+-------------+-----------+
| 2  | Page 2 | 1,2,3     |
+-------------+-----------+
| 3  | Page 3 | 4,5,6     |
+-------------+-----------+

countries
+----+----------------+
| id | name           | 
+----+----------------+
| 1  | United States  |
+----+----------------+
| 2  | United Kingdom |
+----+----------------+
| 3  | Germany        |
+----+----------------+
| 4  | France         |
+----+----------------+
| 5  | Hong Kong      |
+----+----------------+
| 6  | Thailand       |
+----+----------------+
| 7  | Belgium        |
+----+----------------+
| 8  | Singapore      |
+----+----------------+

My model and controller look something like:

country 

public function pages()
{
    return $this->hasMany(Page::class, 'id', 'countries');
}

MemberController.php

$countries = Country::with('pages')->get();

This is returning all countries, but only Page 1 contains any relationships.

Is there a way to retrieve relationships using a whereIn approach so all three countries will return appropriate pages?

Thanks in advance

解决方案

Since Page can belong to many Countries, you need to create a pivot table called country_page and remove the countries column.

Then define two belongsToMany() relationships in both models:

public function pages()
{
    return $this->belongsToMany(Page::class);
}

If you're not following Laravel naming conventions listed in my repo and you gave the pivot name a custom name, define it too:

public function pages()
{
    return $this->belongsToMany(Page::class, 'custom_pivot_table');
}

这篇关于Laravel.如何在外键是数组的情况下获取关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆