В приложении Laravel 6 я создаю оболочку ресурсов для дат, возвращаемых из таблицы Settings
(пары name => value
), но я получил возвращенные данные в недопустимом формате, а не пары name => value
.
У меня есть в контроллере:
$settingsArray= [];
$settingValues= Settings::getSettingsList();
foreach( $settingValues as $nextSettingValue ) {
$settingsArray[$nextSettingValue->name] = $nextSettingValue->value;
}
\Log::info('$settingsArray ::');
\Log::info(print_r( $settingsArray, true ));
return (new SettingsCollection($settingsArray));
Вывод регистрации выше:
[2020-04-21 07:47:07] local.INFO: $settingsArray ::
[2020-04-21 07:47:07] local.INFO: Array
(
[site_name] => VTasks
[copyright_text] => © 2019 - 2020 All rights reserved
[site_heading] => Task 'em all
[backend_items_per_page] => 2
[tasks_per_page] => 4
[events_per_page] => 5
[events_demo_month] => 2018-02-01
)
и в приложении / Http / Controllers / API / Admin / SettingsController. php:
class SettingsCollection extends ResourceCollection
{
public static $wrap = 'settings';
public function toArray($request)
{
\Log::info('SettingsCollection $request::');
\Log::info(print_r( json_encode($request), true ));
\Log::info('+++ SettingsCollection $this::');
\Log::info(print_r( json_encode($this), true ));
return $this->collection->transform(function($settings, $key){
\Log::info('+++ $settings:');
\Log::info(print_r( json_encode($settings), true ));
\Log::info('+++ $key:');
\Log::info(print_r( json_encode($key), true ));
return [
$key => $settings,
];
});
}
Я вижу в выводе журнала:
[2020-04-21 07:47:07] local.INFO: "tasks_per_page"
[2020-04-21 07:47:07] local.INFO: +++ $settings:
[2020-04-21 07:47:07] local.INFO: "5"
[2020-04-21 07:47:07] local.INFO: +++ $key:
[2020-04-21 07:47:07] local.INFO: "events_per_page"
[2020-04-21 07:47:07] local.INFO: +++ $settings:
[2020-04-21 07:47:07] local.INFO: "2018-02-01"
[2020-04-21 07:47:07] local.INFO: +++ $key:
[2020-04-21 07:47:07] local.INFO: "events_demo_month"
[2020-04-21 07:47:07] local.INFO: {"site_name":{"site_name":"VTasks"},"copyright_text":{"copyright_text":"\u00a9 2019 - 2020 All rights reserved"},"site_heading":{"site_heading":"Task 'em all"},"backend_items_per_page":{"backend_items_per_page":"2"},"tasks_per_page":{"tasks_per_page":"4"},"events_per_page":{"events_per_page":"5"},"events_demo_month":{"events_demo_month":"2018-02-01"}}
И возвращенные данные имеют неправильную структуру: https://imgur.com/a/CvIAAQO
Какой правильный путь?