App\Models\Product::toESArray() 方法中对boolean类型的值处理有问题

首先代码中已经加了cast:

protected $cast = [
        'on_sale' => 'boolean',
    ];

但是在tinker中运行教材代码还是提示错误:

>>> app('es')->index(['id' => $arr['id'], 'index' => 'products', 'body' => $arr]);
Elasticsearch\Common\Exceptions\BadRequest400Exception with message '{"error":{"root_cause":[{"type":"mapper_parsing_exception","reason":"failed to parse field [on_sale] of type [boolean] in document with id '29'. Preview of field's value: '1'"}],"type":"mapper_parsing_exception","reason":"failed to parse field [on_sale] of type [boolean] in document with id '29'. Preview of field's value: '1'","caused_by":{"type":"json_parse_exception","reason":"Current token (VALUE_NUMBER_INT) not of boolean type\n at [Source: (ByteArrayInputStream); line: 1, column: 882]"}},"status":400}'
>>> $arr = App\Models\Product::find(29)->toESArray();
=> [
     "id" => 29,
     "type" => "normal",
     "category_id" => 4,
     "title" => "手机存储卡",
     "long_title" => "这是一个超级牛逼的手机存储卡",
     "rating" => 5.0,
     "sold_count" => 0,
     "review_count" => 0,
     "price" => "803.00",
     "category" => [
       "手机配件",
       "存储卡",
     ],
     "category_path" => "-1-",
     "description" => "Praesentium repudiandae a est tenetur molestias dolorem.",
     "skus" => Illuminate\Support\Collection {#4723
       all: [
         [
           "title" => "voluptatem",
           "description" => "Ratione et nulla labore adipisci quia sequi nam.",
           "price" => "8991.00",
         ],
         [
           "title" => "tenetur",
           "description" => "Optio repellat non consequatur iste.",
           "price" => "5831.00",
         ],
         [
           "title" => "dicta",
           "description" => "Harum occaecati nostrum delectus repudiandae animi quasi doloremque.",
           "price" => "803.00",
         ],
       ],
     },
     "properties" => Illuminate\Support\Collection {#4708
       all: [
         [
           "name" => "容量",
           "value" => "32G",
         ],
         [
           "name" => "品牌",
           "value" => "金士顿",
         ],
         [
           "name" => "容量",
           "value" => "64G",
         ],
       ],
     },
     "on_sale" => 1,
   ]

发现”on_sale”的值是1而不是true,有人遇到类似的问题么?我用的ES是7.13.4版本的。

《L01 基础入门》
我们将带你从零开发一个项目并部署到线上,本课程教授 Web 开发中专业、实用的技能,如 Git 工作流、Laravel Mix 前端工作流等。
《L04 微信小程序从零到发布》
从小程序个人账户申请开始,带你一步步进行开发一个微信小程序,直到提交微信控制台上线发布。
leo
最佳答案

casts

2年前 评论
讨论数量: 2
leo

casts

2年前 评论

@leo 眼瘸了,不好意思,确实是这个问题。

2年前 评论

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