简体   繁体   English

在Laravel中循环遍历两个表

[英]Loop through two tables in Laravel

Let's say I have two tables. 假设我有两个桌子。 Table 1 is called "rooms" and table 2 is called "fruits". 表1称为“房间”,表2称为“水果”。 In every room there is a left and a right side and the fruits from table "fruits" are going to be put to the left or right side of the room. 在每个房间中,都有一个左侧和右侧,“水果”表中的水果将被放置在房间的左侧或右侧。

An example: In room "kitchen" we have 2 bananas on the right side and 5 bananas on the left side. 例如:在“厨房”房间中,我们在右侧有2个香蕉,在左侧有5个香蕉。

My tables according the example: 我的表根据示例:

room 房间

id name 身份证名

1 kitchen 1个厨房

2 bedroom 2间卧室

fruits 水果

id | id | name | 名称| 1_right | 1_right | 1_left | 1_left | 2_right | 2_right | 2_left 2_left

1 | 1 | apple | 苹果| 0 | 0 | 0 | 0 | 0 | 0 | 0 0

2 | 2 | banana | 香蕉| 2 | 2 | 5 | 5 | 0 | 0 | 0 0

As you can see "1_right" is "kitchenID_right". 如您所见,“ 1_right”是“ kitchenID_right”。 In the blade view I would like to loop through my "rooms" with the data from "right" and "left": 在刀片视图中,我想使用“ right”和“ left”中的数据遍历“房间”:

Where are bananas? 香蕉在哪里?

  • kitchen right: 2 厨房右边:2
  • kitchen left: 5 厨房左:5
  • bedroom right: 0 卧室右边:0
  • bedroom left: 0 卧室剩余:0

Any ideas on how I can do this with Laravel? 关于如何使用Laravel做到这一点的任何想法? Of course, I can do a sql query in the blade view, but since I know that this is bad practice I was wondering if there is a cleaner way to achieve the same result. 当然,我可以在刀片视图中执行sql查询,但是由于我知道这是一种不好的做法,所以我想知道是否有一种更干净的方法来实现相同的结果。

If your rooms are fixed, you don't need to create rooms table. 如果您的房间是固定的,则无需创建房间表。 Otherwise it's not a good idea to add IDs of room table as field prefix in fruits table. 否则,将房间表的ID添加为水果表中的字段前缀不是一个好主意。 You can not join them in a proper way. 您无法以适当的方式加入他们。 You should alter your tables. 您应该更改表格。 You can create a 3rd -mapping- table. 您可以创建第3个-mapping-表。

eg 例如

room_fruits [table name]
room_id
left_fruit_id
right_fruit_id

You should also use many to many relationship of Eloquent . 您还应该使用Eloquent的多对多关系

First you should re-structure your database as this .. 首先,您应该按以下方式重新构建数据库。

ROOMS 客房

id , name

** FRUITS ** ** 水果 **

id , name , left_qty , right_qty , room_id

Then in your MODEL ROOM 然后在您的模型室中

public function fruits()
{
    return $this->hasMany('App\Fruit');
}

in your MODEL FRUITS 在您的模型水果中

public function room()
{
    return $this->belongsTo('App\Room');
}

so in controller you can do 所以在控制器中你可以做

$rooms = Room::all();
foreach($rooms as $room)
{
    echo $room->name;
    foreach($room->fruits as $fruit)
    {
        echo $fruit->name . ': (right)' . $fruit->right_qty . ' (left)' . $fruit->left_qty;
    }
}

REFERENCE 参考

Just a slight improvisation of what @Koray Küpe said, you can create the mapping table as follows, but keeping the rooms table as it is (since rooms may not always be fixed): @KorayKüpe所说的只是一点点即兴创作,您可以按如下方式创建映射表,但要保持Rooms表不变(因为不一定总是固定房间):

Table: room_fruits
-------------------
id
room_id (from rooms table)
fruit_id (from fruits table)
left (boolean)
right (boolean)

This way it'll be easier for you to select fruits that are only on either left or right room when building your query 这样,您在构建查询时可以更轻松地选择仅位于左侧或右侧空间的水果

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM