Laravel 5.4 将 json 保存到数据库

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46364906/
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 16:41:59  来源:igfitidea点击:

Laravel 5.4 save json to database

phpjsonlaravel

提问by tosky

help me with save json to db. Table field type - text. I have Model with cast array

帮我将 json 保存到 db。表字段类型 - 文本。我有带转换数组的模型

class Salesteam extends Model 
{
    protected $casts = [        
        'team_members' => 'array'
    ];
}

I want get json like this {"index":"userId"} and store it to db. Example:

我想得到这样的 json {"index":"userId"} 并将其存储到 db。例子:

{"1":"7","2":"14","3":"25","4":"11"}

I have Salesteam Model and 'team_members' column in db to saving all user id's belong to this team. My code find team by id, get all 'team_members' attribute and add new user to this team. I want store all users in json format and incrementing index when add new user id.

我在数据库中有 Salesteam 模型和“team_members”列,用于保存属于该团队的所有用户 ID。我的代码通过 id 查找团队,获取所有“team_members”属性并将新用户添加到该团队。我想在添加新用户 ID 时以 json 格式存储所有用户并增加索引。

Salesteam attributes:

销售团队属性:

"id" => 20
"salesteam" => "Admin"
"team_leader" => 0
"invoice_target" => 884.0
"invoice_forecast" => 235.0
"team_members" => "[1,66,71]"
"leads" => 0
"quotations" => 0
"opportunities" => 0
"notes" => ""
"user_id" => 1
"created_at" => "2017-09-22 09:13:33"
"updated_at" => "2017-09-22 13:10:25"
"deleted_at" => null

Code to add new user to team:

将新用户添加到团队的代码:

$salesTeam = $this->model->find($salesTeamId);
$teamMembers = $salesTeam->team_members;
$teamMembers[] = $userId;
$salesTeam->team_members = $teamMembers;
$salesTeam->save();

But it stores to db array and without index

但它存储到 db 数组并且没有索引

"team_members" => "[1,34,56]"

From Laravel documentation - Array & JSON Casting"array will automatically be serialized back into JSON for storage"

来自 Laravel 文档 - Array & JSON Casting“数组将自动序列化回 JSON 进行存储”

Where i'm wrong?

我错在哪里?

回答by rummykhan

To make a json string like this

像这样制作一个json字符串

{"1":"7","2":"14","3":"25","4":"11"}

In php, you must pass a key, when you're pushing elements to array. e.g.

在 中php,当您将元素推送到数组时,您必须传递一个键。例如

$teamMembers = [];
$teamMembers['1'] = 7;
$teamMembers['2'] = 14;
...

If you don't make it an associative array, phpwill consider it as json array when converting to json string..

如果您不将其设为associative array,php将在转换为 json 字符串时将其视为 json 数组..

Tip:You may pass only one key and it will work too. e.g.

提示:您可以只传递一个键,它也会起作用。例如

$test = [1, 2, 3, 4, 5, 6];

$test['10'] = 56;

echo json_encode($test);

Output:

输出:

"{"0":1,"1":2,"2":3,"3":4,"4":5,"5":6,"10":56}"

PHP-Reference

PHP-参考

回答by BugraDayi

Simple Solution

简单的解决方案

$books = App\Book::all();
$user->books = $books->toJson();
$user->save(); //example: userid -> 2

$user = App\User::find(2);
$userBooks = json_decode($user->books, true); // to generate object again

回答by omar jayed

Simply use

只需使用

$teamMembers = json_encode($salesTeam->team_members);

That should do be all.

这应该是全部。

回答by Onix

if you get this "[1,34,56]"just do json_decode($team_members)

如果你得到这个 "[1,34,56]"就做json_decode($team_members)

and you will have this:

你会得到这个:

array:3 [▼
  0 => 1
  1 => 34
  2 => 56
]