【问题标题】:Return resource in a specific pattern [duplicate]以特定模式返回资源[重复]
【发布时间】:2022-01-23 16:11:03
【问题描述】:

我正在尝试让我的资源以这种方式返回数据(1) 2020-11-26 / 2020-11-27 (id) data_from / data_to 有什么办法吗?

public function toArray($request)
{
    $parent_array = parent::toArray($request);

    $return_array = [];
    $return_array['id'] = $parent_array['id'];
    $return_array['text'] = $parent_array['data_from'];
    $return_array['text2'] = $parent_array['data_to'];

    return $return_array;
}
{
    "response": {
        "id": 1,
        "text": "2020-11-26",
        "text2": "2020-11-27"
    }
}

【问题讨论】:

标签: php laravel resources


【解决方案1】:
return sprintf('(%s) %s / %s', $parent_array['id'], $parent_array['data_from'], $parent_array['data_to']);

https://www.php.net/manual/en/function.sprintf.php

字符串元素的基本连接也可以$string1 . ' ' . $string2

但不确定您是否也想要现有值。

接收数据的内容也可以将其格式化为字符串

【讨论】:

  • 请帮助关闭 2022 年的基本重复问题。我们已经为所有基本问题提供了至少 5 页。是时候整合 Stack Overflow 上的内容了。
  • 我使用 $return_array['text'] = "($id) " 解决了。 $this->data_from->format('Y-m-d') 。 “/”。 $this->data_to->format('Y-m-d');
猜你喜欢
  • 2020-05-09
  • 2011-10-22
  • 2022-01-22
  • 2017-03-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-15
  • 2012-09-16
相关资源
最近更新 更多