0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【Laravel】apiで一覧取得結果のjsonを返したら、ダブルクォーテーションがエスケープされた

Posted at

事象

Eloquentを使い検索結果のリストをjsonに変換後、return response()->jsonで返却したところvalue部分のダブルクォーテーションがエスケープされました。

"result": "[{"id":2,"status_type":-10,"name":"aaaaa"…]"

本当はこんな風に返してほしいのですが

"result": "[{"id":2,"status_type":-10,"name":"aaaaa"…]"

環境

Laravel Framework 8.43.0
PHP 7.3.24-(to be removed in future macOS) (cli) (built: May  8 2021 09:40:34) ( NTS )

解決策

一覧取得時に->toJson()を使ったのが駄目?だったようです
こんな感じで書いていました。
$items = Hoge::select()->orderBy('created_at', 'desc')->get()->toJson();

下の書き方で上手く行きました。
$items = Hoge::select()->orderBy('created_at', 'desc')->get(); json_encode($items);

理由はよくわかりません。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?