在 Php/Laravel/msql 中连接兄弟姐妹之间的关系

时间:2021-07-01 22:48:51

标签: php laravel laravel-query-builder laravel-collection

我目前正在建立兄弟姐妹之间的关系。

问题:如果我在有兄弟姐妹列表的主要人物并且有记录到数据库,但是每当我去兄弟姐妹之一时,我只能查看主要人物,而不能查看主要人物其他兄弟姐妹。

字母代表兄弟姐妹 第一个场景 - (a : b,c,d,e,f) 第二种情况 - ( b : a )

预期输出:每当我转到第二个兄弟姐妹时,在本例中为“b”,理想情况下它会显示列表中的其他兄弟姐妹。

字母代表兄弟姐妹 第一个场景 - (a : b,c,d,e,f) 第二种情况 - ( b : a,c,d,e,f )

这是我的代码

这个是获取主person下兄弟姐妹列表,传入数组

$sibling_list = [$tree['id']];
    
    $sibling_list_items = YommFtRelationship::whereIn('type_id', $sibling_ids)
    ->where(function ($query) use ($sibling_list) {
        $query->whereIn('member_1_id', $sibling_list)
            ->orWhereIn('member_2_id', $sibling_list);
    })
    ->distinct()
    ->get(['member_1_id', 'member_2_id']);

    foreach ($sibling_list_items as $sibling) {
        $sibling_list[$sibling->member_1_id] = $sibling->member_1_id;
        $sibling_list[$sibling->member_2_id] = $sibling->member_2_id;
    }

此代码是为了确保$sibling_list 等于另一个表上的成员

         $mergeSibling = DB::table('yomm_ft_relationships AS r')
     ->join('yomm_ft_members AS m', 'm.id', '=', 'r.member_1_id')
     ->whereIn('m.id', $sibling_list)
     ->get();

我正在尝试调用一个函数来创建 $sibling_list 中兄弟姐妹之间的关系,$sibling_list 是一个来自 foreach 结果的数组。

         $mergeSibling->each (function (YommFtRelationship $relationship) use ($sibling_list, $is_biological, $sibling_ids){
         $this-> createRelationshipBetween(
            $sibling_list->getKey(),
            $sibling_list->getKey(),
            $sibling_ids,
            $gender,
            $is_biological);         
        });

我的流程是否正确。干杯小伙子

根据要求,这些是我的桌子

Relationship table
id
type_id
member_1_id
member_2_id
biological
marital_status
status
created_at
updated_at

Relationship types table
id
type
name
created_at
updated_at

ID  type       Name
1   mother     Mother
2   father     Father
5   sister     Sister
6   brother    Brother
7   daughter   Daughter
8   son        Son
9   peer       Peer
10  partner    Partner
11  caregiver  Caregiver
12  whangai    Whangai

Rembers table
id
region_id
visitor_id
last_name
first_name
middle_name
preferred_name
NHI
dob
deceased_at
sex_id
gender_id
sexuality_id
created_at
updated_at

0 个答案:

没有答案