请教,从两张表分别取出数据,然后组合后按日期分组输出,这个怎么写啊?

laravel后端从两张表分别取出数据,然后组合后按日期分组输出,这个怎么写啊?

1:notes取出来的数是
array:2 [
  0 => {#337
    +"id": 1
    +"title": "房贷还款"
    +"date": "2021-09-18"
    +"content": "房贷8688元还款"
  }
  1 => {#327
    +"id": 2
    +"title": "购车材料"
    +"date": "2021-09-19"
    +"content": "购车材料提交截止时间"
  }
]2:other
array:2 [ `0 => {#333 `+"id": 1
    +"name": "交割"
    +"title": "黄金期货交割"
    +"date": "2021-09-18"
    +"content": "第五期黄金期货交割日"
}
  1 => {#332 `+"id": 2
    +"name": "打新"
    +"title": "两只新股打新"
    +"date": "2021-09-19"
    +"content": "大盘新股打新时间"
 } ]

如何组合成按日期分组的数组输出,像如下样式:

'2021-09-18' => [
    'notes => [
        'id' => '1',
        'title' => '部门会议',
        'content' => '下午四点开部门会议',
    ],
    'other' => [
        'list' => [
            [   'id' => '1',
                'name' =>'交割',
                'title' => '黄金期货交割',
                'content' => '第五期黄金期货交割日',
             ],
        ]
    ]
],
'2021-09-19' => [
    'notes' => [
        'id' => '2',
        'title' => '团队聚会',
        'content' => '中秋节搞团聚',
    ],
    'other' => [
        'list' => [
            [   'id' => '2',
                'name' =>'打新',
                'title' => '两只新股打新',
                'content' => '大盘新股打新时间',
 ],
        ]
    ]
]

```

《L05 电商实战》
从零开发一个电商项目,功能包括电商后台、商品 & SKU 管理、购物车、订单管理、支付宝支付、微信支付、订单退款流程、优惠券等
《L03 构架 API 服务器》
你将学到如 RESTFul 设计风格、PostMan 的使用、OAuth 流程,JWT 概念及使用 和 API 开发相关的进阶知识。
讨论数量: 6
陈先生

建议自行冗余字段,并添加索引

2年前 评论

分别取出,在 PHP中 处理,foreach 就行

2年前 评论
yizhu (楼主) 2年前
$arr1 = [
    0 => [
        "id"=>1,
        "title" =>"房贷还款",
        "date" => "2021-09-18",
        "content" => "房贷8688元还款"
    ],
    1 => [
        "id"=>2,
        "title" =>"购车材料",
        "date" => "2021-09-19",
        "content" => "房贷8688元还款"
    ]
];

$arr2 = [
    0 => [
        "name"=>"交割",
        "title" =>"黄金期货交割",
        "date" => "2021-09-18",
        "content" => "第五期黄金期货交割日"
    ],
    1 => [
        "name"=>"打新",
        "title" =>"两只新股打新",
        "date" => "2021-09-19",
        "content" => "大盘新股打新时间"
    ]
];
// 1.先把一张表的所有的日期取出来。
// 保存在一个数组中。
$date = ["2021-09-18","2021-09-19"];
$a1 = [];
// 2.遍历日期数组。
foreach($date as $date){
    // 遍历$arr1数组。
    foreach($arr1 as $key => $value){
        if($value['date'] == $date){
            $a1[$date]['note'] = $value; 
        }
    }
    // 遍历$arr2数组。
    foreach($arr2 as $key => $value){
        if($value['date'] == $date){
            $a1[$date]['others']['list'] = $value; 
        }
    }
}

结果

array(2) {
  ["2021-09-18"]=>
  array(2) {
    ["note"]=>
    array(4) {
      ["id"]=>
      int(1)
      ["title"]=>
      string(12) "房贷还款"
      ["date"]=>
      string(10) "2021-09-18"
      ["content"]=>
      string(19) "房贷8688元还款"
    }
    ["others"]=>
    array(1) {
      ["list"]=>
      array(4) {
        ["name"]=>
        string(6) "交割"
        ["title"]=>
        string(18) "黄金期货交割"
        ["date"]=>
        string(10) "2021-09-18"
        ["content"]=>
        string(30) "第五期黄金期货交割日"
      }
    }
  }
  ["2021-09-19"]=>
  array(2) {
    ["note"]=>
    array(4) {
      ["id"]=>
      int(2)
      ["title"]=>
      string(12) "购车材料"
      ["date"]=>
      string(10) "2021-09-19"
      ["content"]=>
      string(19) "房贷8688元还款"
    }
    ["others"]=>
    array(1) {
      ["list"]=>
      array(4) {
        ["name"]=>
        string(6) "打新"
        ["title"]=>
        string(18) "两只新股打新"
        ["date"]=>
        string(10) "2021-09-19"
        ["content"]=>
        string(24) "大盘新股打新时间"
      }
    }
  }
}
2年前 评论

这个表设计的也有点问题,靠时间去关联在一起明显是会有问题的。就像一楼说的,需要添加冗余字段,比如都通过user_id关联到用户信息,那么就很简单了:
User模型分别写关联关系,假设都是一对多:

public function notes{
    return $this->hasMany(Note::class,"user_id","id");
}
public function other{
    return $this->hasMany(Other::class,"user_id","id");
}

然后查询的时候就可以这样:

$list = User::where("查询条件")->with(["notes","other"])->get();

不仅可以精准查找到所有的信息,还可以解决n+1的问题,好了,如果要按照时间查找,可以这样:

$users = User::with(['notes' => function ($query) {
    $query->where('title', 'like', '%code%');
}])->get();

或者可以直接查询出所有的集合,然后在集合中用时间筛选就好。

2年前 评论
随波逐流

集合合并, 在分组

2年前 评论
// get date list
$mapDateToData = array_flip(
    array_merge(
        array_column($notes, 'date'), 
        array_column($others, 'date')
    )
);

// init
foreach ($mapDateToData as $k => $v) {
    $mapDateToData[$k] = [
        'notes' => [],
        'other' => [
            'list' => [],
        ],
    ];
}

// set note
foreach ($notes as $note) {
    $mapDateToData[$note['date']]['notes'] = $note;
}

// set other
foreach ($others as $other) {
    $mapDateToData[$other['date']]['other']['list'][] = $other;
}

// get data
$data = array_values($mapDateToData);
2年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!