将来自 API 请求的热门类别存储到数据库中

Store top categories from an API request into the database

我有一个这样的 API 请求:

$postInformation = 
(new postInformation)->handle(['field' => 'slug', 'value' => $this->slug]);

除其他内容外,此响应的转储显示了来自此 API 请求的一些类别。它们采用这种格式,其中有一个带有顶级类别的 'categories' 键:

categories:
^ array:40 [
  "id" => 2
  ...
  "categories" => array:2 [
    "data" => array:15 [
      0 => array:3 [
        "id" => 6
        "name" => array:1 [
          "en" => "General"
        ]
        "on_request" => 0
      ]
      1 => array:3 [
        "id" => 14
        "name" => array:1 [
          "en" => "Tuts"
        ]
        "on_request" => 0
      ]
      2 => array:3 [
        "id" => 3
        "name" => array:1 [
          "en" => "Laravel"
        ]
        "on_request" => 0
      ]
      
      ...
  ]
  ...
 ]

我创建了一个 table 'post_top_categories' 和一个模型 PostTopCategory,我想从上面的 API 响应中获取热门类别并将它们存储到 'post_top_categories' table。但我不明白如何正确地实现这一目标。你知道它是如何实现的吗?谢谢

foreach($yourArray['categories']['data'] as $topCategory)
{
    $catId = $topCategory['id'];
    $catname = $topCategory['name']['en'];
    $catOnRequest = $topCategory['on_request'];
    // Do what you want with those values now
}