How to fully convert the json returned by laravel to a string

Source: Internet
Author: User
For example, if the age type in the database is int, $ arr [& #039; age & #039; & amp; gt; 20, & #039; id & #039; & amp; gt; 100]; The json returned by Response: json ($ arr) is {& quot; id & quot;: 100, & quot; age & quot ;: 20}, except for setting $ casts [& #039; age & #039; & amp; gt; & #039; string & #039;] In the model how can I convert the data type to {& quot; id & quot ;:... for example, if the age type in the database is int, $ arr = ['age' => 20, 'id' => 100] is found. Response::json($arr)The returned json is {"id":100,"age":20}In addition to setting $casts=['age'=>'string']How can I convert data types {"id":"100","age":"20"}? Because too many fields need to be set

Reply content:

For example, if the age type in the database is int, $ arr = ['age' => 20, 'id' => 100] is found.Response::json($arr)The returned json is{"id":100,"age":20}In addition to setting$casts=['age'=>'string']How can I convert data types{"id":"100","age":"20"}? Because too many fields need to be set

Json_dump

phpclass Model extends Eloquent{    public function toArray()    {        return array_map(function($a) {            return strval($a);        }, parent::toArray());    }}
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.