在 laravel 中查询时创建一个包含子元素的数组

Create an array containing child elements when querying in laravel

我正在研究 laravel。我有 2 mysql tables:

table: 日志

id   name    description     status
1   login    login system    success
2   create   create user     success
3   update   update user     success

table: log_detail

id    log_id     old_val        new_val
1       3         admin           user

在控制器中LogController.php 我查询:

$list = DB::table('log')
            ->leftJoin('log_detail', 'log.id', '=', 'log_detail.log_id')
            ->selectRaw("log.name, log.description, log.status, log_detail.oldVal, log_detail.newVal")
            ->get()
            ->toArray();

// 结果

array(11) {
        [0]=>
        object(stdClass)#1377 (3) {
          ["name"]=>
          string(6) "login"
          ["description"]=>
          string(19) "login system"
          ["status"]=>
          string(6) "success"
          ["oldVal"]=> NULL
          ["newVal"]=> NULL
        }
        [1]=>
        object(stdClass)#1377 (3) {
            ["name"]=>
            string(6) "create"
            ["description"]=>
            string(19) "create user"
            ["status"]=>
            string(6) "success"
            ["oldVal"]=> NULL
            ["newVal"]=> NULL
          }
        [2]=>
        object(stdClass)#1377 (3) {
            ["name"]=>
            string(6) "update"
            ["description"]=>
            string(19) "update user"
            ["status"]=>
            string(6) "success"
            ["oldVal"]=>
            string(7) "admin"
            ["newVal"]=>
            string(6) "user"
          }
      }

结果就是我想要的..现在我希望 log_detail table 中的结果在一个数组中。我想要的示例输出:

      object(stdClass)#1377 (3) {
        ["name"]=>
        string(6) "update"
        ["description"]=>
        string(19) "update user"
        ["status"]=>
        string(6) "success"
        ["data"]=> [
            ["oldVal"]=>
            string(6) "admin"
            ["newVal"]=>
            string(6) "user"
        ]
      }

请给我你的意见。感谢您阅读。谢谢

您可以映射您的结果以获得所需的输出:

$list = DB::table('log')
            ->leftJoin('log_detail', 'log.id', '=', 'log_detail.log_id')
            ->selectRaw("log.name, log.description, log.status, log_detail.oldVal, log_detail.newVal")
            ->get()
            ->toArray();

$mappedList = $list->map(function ($record) {
        $record['data'] = [
                'oldVal' => $record['oldVal'],
                'newVal' => $record['newVal'],
            ];
        unset($record['oldVal'], $record['newVal']);
        
        return $record;
    });

您可以在 documentation 中阅读更多相关信息。

PS:你最好用Relationships。这样一来,您就无需在需要提取日志和日志详细信息的任何地方手动加入数据。

我建议为此使用 Laravel eloquent,因为 eloquent 提供类似的响应。

在模型中创建关系的第一步。

// In your Log.php

public function details(){
  return $this->hasMany(Detail::class);
}
// In your Detail.php

public function log(){
  return $this->belongsTo(Log::class);
}

然后使用eloquent通话记录如下

$logs = Log::with('details')->get();

每个日志中都有 ->details 对象,可以像下面这样访问

foreach($logs as $log){
   dump($log->details); // you'll see all the details table columns here under attribute
}

Laravel 文档:https://laravel.com/docs/9.x/eloquent-relationships#one-to-many

按照此文档在 laravel eloquent.

中创建关系