简体   繁体   English

Laravel 5.4 将 json 保存到数据库

[英]Laravel 5.4 save json to database

help me with save json to db.帮我将 json 保存到 db。 Table field type - text.表字段类型 - 文本。 I have Model with cast array我有带转换数组的模型

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

I want get json like this {"index":"userId"} and store it to db.我想得到这样的 json {"index":"userId"} 并将其存储到 db。 Example:示例:

{"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.我在数据库中有 Salesteam 模型和“team_members”列来保存属于这个团队的所有用户 ID。 My code find team by id, get all 'team_members' attribute and add new user to this team.我的代码通过 id 查找团队,获取所有“team_members”属性并将新用户添加到该团队。 I want store all users in json format and incrementing index when add new user id.我想在添加新用户 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?我错在哪里?

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.php ,当您将元素推送到数组时,您必须传递一个键。 eg例如

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

If you don't make it an associative array , php will consider it as json array when converting to json string..如果你不让它成为一个associative arrayphp在转换为 json 字符串时会认为它是 json 数组。

Tip: You may pass only one key and it will work too.提示:您可以只传递一个键,它也会起作用。 eg例如

$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-参考

Simply use只需使用

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

That should do be all.这应该是全部。

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
]

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM