#php #arrays #json #laravel #jsondecoder
#php #массивы #json #laravel #jsondecoder
Вопрос:
Я должен преобразовать json в arry из поля таблицы уведомлений laravel data
,
Это данные, показанные на dd($user->notifications);
Вот как ответ отображается в postman
Мне нужно показать полезную нагрузку внутри []
, как показано ниже
{
"payload": [ {
"title": "PHP Developer Accepted",
"description": "<p>This is a professional IT Jobamp;nbsp;</p>",
"user_id": 54,
"job_id": "01"
}],
"message": "",
"result": true
}
Вот моя индексная функция контроллера
protected function index()
{
$user = DeviceAuthenticator::getUserByAccessToken();
foreach ($user->notifications as $notification) {
$notifications = $notification->data;
}
return response()->apiSuccess($notifications);
}
Но прежде чем я дамп $notifications перед ответом, он отображается как массив
dd($notifications);
Ответ №1:
просто вставьте []
свою переменную, которая будет выглядеть
protected function index()
{
$user = DeviceAuthenticator::getUserByAccessToken();
foreach ($user->notifications as $notification) {
$notifications = $notification->data;
}
return response()->apiSuccess([$notifications]); // wrap in array
}
Вам может понадобиться что-то вроде этого
protected function index()
{
$user = DeviceAuthenticator::getUserByAccessToken();
$notifications = [];
foreach ($user->notifications as $notification) {
$notifications[] = $notification->data;
}
return response()->apiSuccess($notifications);
}
вот notifications
массив
Ответ №2:
Вы можете попробовать добавить this
-> toArray();