JSON字段保存问题?

protected $casts = [
    "address" => "array",
];
//原数据
{
    "name": "广州市青年公园",
    "lat": 23.12685742223636,
    "lng": 113.2290568479727
}
//保存后的数据
{"name":"\u5e7f\u5dde\u5e02\u9752\u5e74\u516c\u56ed","lat":23.12685742223636,"lng":113.2290568479727}

模型中字段配置为array,保存时字段中的中文自动转成了unicode码,业务中需要使用模糊查询字段的name属性,如何修改使保存时中文不转成unicode码

《L04 微信小程序从零到发布》
从小程序个人账户申请开始,带你一步步进行开发一个微信小程序,直到提交微信控制台上线发布。
《L03 构架 API 服务器》
你将学到如 RESTFul 设计风格、PostMan 的使用、OAuth 流程,JWT 概念及使用 和 API 开发相关的进阶知识。
最佳答案

已使用自定义cast实现,下面为实现代码

//app/Casts/Json.php
<?php

namespace App\Casts;

use Illuminate\Contracts\Database\Eloquent\CastsAttributes;
use Illuminate\Database\Eloquent\Model;

/**
 * 模型字段转成,保存json时中文不转成unicode
 */
class Json implements CastsAttributes
{
    public function get(Model $model, string $key, mixed $value, array $attributes): mixed
    {
        return json_decode($value, true);
    }
    public function set(Model $model, string $key, mixed $value, array $attributes): mixed
    {
        return json_encode($value, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
    }
}

//app/Models/ShopModel.php
class ShopModel extends Model
{
    protected $casts = [
        "address"                => Json::class
    ];
}
4个月前 评论
讨论数量: 7

已使用自定义cast实现,下面为实现代码

//app/Casts/Json.php
<?php

namespace App\Casts;

use Illuminate\Contracts\Database\Eloquent\CastsAttributes;
use Illuminate\Database\Eloquent\Model;

/**
 * 模型字段转成,保存json时中文不转成unicode
 */
class Json implements CastsAttributes
{
    public function get(Model $model, string $key, mixed $value, array $attributes): mixed
    {
        return json_decode($value, true);
    }
    public function set(Model $model, string $key, mixed $value, array $attributes): mixed
    {
        return json_encode($value, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
    }
}

//app/Models/ShopModel.php
class ShopModel extends Model
{
    protected $casts = [
        "address"                => Json::class
    ];
}
4个月前 评论

写 setAttribute 方法,里面自己json_decode,设置不转换。或者是like查询的时候把中文转换成,然后再查

4个月前 评论

已使用自定义cast实现,下面为实现代码

//app/Casts/Json.php
<?php

namespace App\Casts;

use Illuminate\Contracts\Database\Eloquent\CastsAttributes;
use Illuminate\Database\Eloquent\Model;

/**
 * 模型字段转成,保存json时中文不转成unicode
 */
class Json implements CastsAttributes
{
    public function get(Model $model, string $key, mixed $value, array $attributes): mixed
    {
        return json_decode($value, true);
    }
    public function set(Model $model, string $key, mixed $value, array $attributes): mixed
    {
        return json_encode($value, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
    }
}

//app/Models/ShopModel.php
class ShopModel extends Model
{
    protected $casts = [
        "address"                => Json::class
    ];
}
4个月前 评论

如果你在用 MySQL 5.7.5+ 的版本,你可以把字段设置成 JSON 类型,然后创建虚拟列索引,使用 JSON 的方式来查询,从而达到最大效率使用索引是最好的。

当然,如果你要用 like %foo% 这种查询的话,那就无所谓了。

3个月前 评论

Json::encode 支持自定义实现,在服务提供者的 boot 方法中添加以下代码(未测试)

Json::encodeUsing(function($value) {
    return json_encode($value, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
})

file

3个月前 评论
houguang (楼主) 3个月前

可以定义一个基础模型BaseModel ,重写方法asJson

<?php
namespace App;
use \Illuminate\Database\Eloquent\Model;

class BaseModel extends Model
{

    protected function asJson($value)
    {
        return json_encode($value, JSON_UNESCAPED_UNICODE);
    }
}
3个月前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!