laravel 在laravel中保存JSON字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34516590/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 12:55:33  来源:igfitidea点击:

saving JSON string in laravel

phpjsonlaraveleloquentlaravel-5.1

提问by nirvair

I am trying to save a JSON string to the database.

我正在尝试将 JSON 字符串保存到数据库中。

this is the error that I get:

这是我得到的错误:

exception 'ErrorException' with message 'preg_replace(): Parameter mismatch, pattern is a string while replacement is an array

And this is how my JSON looks like:

这就是我的 JSON 的样子:

"metadata": {
      "is_ivr": 0,
      "is_upgradable": 1,
      "is_sms": 0,
      "is_design": 0,
      "threshold_amount": 0,
      "post_threshold": 0,
      "pre_threshold": 0
    }

I want to save the value of metadata as string.

我想将元数据的值保存为字符串。

I tried doing this:

我试过这样做:

$data = $request->input('data');
$data['metadata'] = json_encode($data['metadata']);

Result of dd($data);

的结果 dd($data);

array:8 [
  "product_name" => "Pulse"
  "parent_product" => 0
  "product_description" => "goes here"
  "metadata" => array:7 [
    "is_ivr" => 0
    "is_upgradable" => 1
    "is_sms" => 0
    "is_design" => 0
    "threshold_amount" => 0
    "post_threshold" => 0
    "pre_threshold" => 0
  ]
  "price_period" => 1
  "price_variation" => 2
  "outlet_pricing" => 0
  "status" => 1
  ]
]

回答by Marcin Nabia?ek

To be honest I don't know what's going on at the moment, but open your model (I don't know its name because you haven't showed it) and fill:

老实说我现在不知道发生了什么,但是打开你的模型(我不知道它的名字,因为你没有展示它)并填写:

protected $casts = [
    'metadata' => 'array',
];

assuming you haven't done it yet.

假设你还没有这样做。

Now, when you insert data into database don't use any json_encode, simply you can use:

现在,当您将数据插入数据库时​​,不要使用 any json_encode,只需使用:

$data = $request->input('data');

without:

没有:

$data['metadata'] = json_encode($data['metadata']);

Using castsproperty Laravel will automatically convert array data to json when inserting into database and automatically run json_decodeto get array when getting data from database.

使用casts属性 Laravel 在插入数据库时​​会自动将数组数据转换为 json 并json_decode在从数据库获取数据时自动运行获取数组。

回答by Crisoforo Gaspar

You need to serialize your data before save the JSON into the DB for example:

您需要在将 JSON 保存到数据库之前序列化您的数据,例如:

$data = serialize( json_encode( $data['metadata'] ) );
// Insert $data into the DB

Now if you want to reuse the data in PHP from the DB you need to run the opposite unserialize.

现在,如果您想从 DB 中重用 PHP 中的数据,则需要运行相反的反序列化。

$raw_data = // query to get the data from the DB
$data = unserialize( $raw_data );

This will allow you to save data structures such as arrays. From the PHP documentation

这将允许您保存数据结构,例如数组。来自 PHP 文档

Generates a storable representation of a value.

生成值的可存储表示。

回答by Sari Yono

use Implode();and separate it by commas or if you would like you can access the property of the array by calling its index directly $data['metadata'][index]

使用Implode();并用逗号分隔,或者如果您愿意,可以通过直接调用其索引来访问数组的属性$data['metadata'][index]

whats happening is you are calling a regex on an array not a value.

发生的事情是您在数组上调用正则表达式而不是值。