Mysql-laravelphp data storage ArraytoJson

Source: Internet
Author: User
{Code ...} $ the data from goods is a set of arrays, which are saved to the database as an Array, and then converted to {code...} using json ...} toJson () reports the error CalltoamemberfunctiontoJson () onarray. Can you give me some guidance?
$ Goods = ""; foreach ($ carts as $ key => $ c) {$ good = Goods: where (["product_id" => $ c-> product_id, "merchant_id" => $ c-> merchant_id])-> first (); $ good-> number = $ c-> number; $ goods [] = $ good ;} $ order = new Order; $ order-> json = $ goods; if ($ order-> save () {return response () -> json (array ('status' => 1, 'msg '=> "order successful");} else {return Redirect: back ()-> withInput () -> withErrors ('failed to save! ');}

$ Goods's data is an array.

Saved to the database is an Array.

Use json to convert

$goods = $goods->toJson();

ToJson () Error

Call to a member function toJson () on array

Can you give me some guidance?

Reply content:
$ Goods = ""; foreach ($ carts as $ key => $ c) {$ good = Goods: where (["product_id" => $ c-> product_id, "merchant_id" => $ c-> merchant_id])-> first (); $ good-> number = $ c-> number; $ goods [] = $ good ;} $ order = new Order; $ order-> json = $ goods; if ($ order-> save () {return response () -> json (array ('status' => 1, 'msg '=> "order successful");} else {return Redirect: back ()-> withInput () -> withErrors ('failed to save! ');}

$ Goods's data is an array.

Saved to the database is an Array.

Use json to convert

$goods = $goods->toJson();

ToJson () Error

Call to a member function toJson () on array

Can you give me some guidance?

From yourforeachLook, the last$goodsIt is a common array, which correspondstoJSONThere is no bound relationship. In my memory,toJSONIt should not be built in php.

Proposed solution:

$order -> json = json_encode($goods);

For the above Code, we also have the following suggestions:

foreach($carts as $key => $c){    $good = Goods::where(["product_id" => $c->product_id,"merchant_id" => $c->merchant_id])->first();    $good -> number = $c->number;    $goods[] = $good;}

In the above section, I think you should take a good look at the Relationship part of Eloquent. This should be a good solution.

Second, else keywords are usually not needed:

If ($ order-> save () {return response ()-> json (array ('status' => 1, 'msg '=> "order successful");} return Redirect: back ()-> withInput ()-> withErrors (' Save failed! ');

The above code should also work.

Json_encode () is one of the most commonly used built-in functions in php. lz should not be too dependent on the framework.

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.