Laravel:如何在数据库中存储json格式数据?

时间:2022-02-28 16:58:26

I want to store some json format data in database from a given array. But How i do store it using controller.

我想将一些json格式数据存储在给定数组的数据库中。但我如何使用控制器存储它。

Suppose I have json data like :

假设我有json数据,如:

{"id":"bangladesh","divisions":[{"name":"Barisal"},{"name":"Chittagong"},{"name":"Dhaka"},{"name":"Khulna"},{"name":"Rajshahi"},{"name":"Rangpur"},{"name":"Sylhet"}]}

So far as I know in controller using json format is like this as I'm not fully aware of it.

据我所知,在控制器中使用json格式是这样的,因为我还没有完全清楚它。

public function saveUser(Request $request)
        {
            $div=new Div();
                       $user->json = json_encode( array({"Date 1": {
                 {"id":"bangladesh","divisions":[{"name":"Barisal"},{"name":"Chittagong"},{"name":"Dhaka"},{"name":"Khulna"},{"name":"Rajshahi"},{"name":"Rangpur"},{"name":"Sylhet"}]}   
            $div->save();

            return redirect('getList');
        }

How do i save it in mySQL only the list of divisions in Division Model using Laravel Controller?

如何使用Laravel Controller将它保存在mySQL中仅使用Division Model中的分区列表?

2 个解决方案

#1


4  

You don't need to convert the array data to a JSON string yourself, use the Laravel $casts parameter on your model: https://laravel.com/docs/5.2/eloquent-mutators#attribute-casting

您不需要自己将数组数据转换为JSON字符串,在模型上使用Laravel $ casts参数:https://laravel.com/docs/5.2/eloquent-mutators#attribute-casting

You should do something like this in your Div model:

您应该在Div模型中执行以下操作:

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

#2


0  

You can convert your Model to JSON format like $model->toJson();

您可以将Model转换为JSON格式,如$ model-> toJson();

or

要么

if you have data in an array you can use json_encode(['id' => 1, 'name' => 'User 1']);

如果数组中有数据,可以使用json_encode(['id'=> 1,'name'=>'User 1']);

Laravel Schema does support JSON field types as well look https://laravel.com/docs/5.0/schema#adding-columns

Laravel Schema确实支持JSON字段类型以及https://laravel.com/docs/5.0/schema#adding-columns

You can use text field type as well to store JSON data.

您也可以使用文本字段类型来存储JSON数据。

#1


4  

You don't need to convert the array data to a JSON string yourself, use the Laravel $casts parameter on your model: https://laravel.com/docs/5.2/eloquent-mutators#attribute-casting

您不需要自己将数组数据转换为JSON字符串,在模型上使用Laravel $ casts参数:https://laravel.com/docs/5.2/eloquent-mutators#attribute-casting

You should do something like this in your Div model:

您应该在Div模型中执行以下操作:

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

#2


0  

You can convert your Model to JSON format like $model->toJson();

您可以将Model转换为JSON格式,如$ model-> toJson();

or

要么

if you have data in an array you can use json_encode(['id' => 1, 'name' => 'User 1']);

如果数组中有数据,可以使用json_encode(['id'=> 1,'name'=>'User 1']);

Laravel Schema does support JSON field types as well look https://laravel.com/docs/5.0/schema#adding-columns

Laravel Schema确实支持JSON字段类型以及https://laravel.com/docs/5.0/schema#adding-columns

You can use text field type as well to store JSON data.

您也可以使用文本字段类型来存储JSON数据。