是不是应该加个 JSON_encode ()?

修改过的代码如下:

$order = new Order([
    'address' => json_encode([
        'address'       => $address->full_address,
        'zip'           => $address->zip,
        'contact_name'  => $address->contact_name,
        'contact_phone' => $address->contact_phone,
    ]),
    'remark'       => $request->input('remark'),
    'total_amount' => 0,
]);
$order->user()->associate($user);
$order->save();

没加json_encode()之前一直报错,错误日志如下:

local.ERROR: Type error: Argument 1 passed to Illuminate\Database\Grammar::parameterize() must be of the type array, string given, called in /mnt/d/WWW/learn/laravel/laravel-shop/vendor/laravel/framework/src/Illuminate/Database/Query/Grammars/Grammar.php on line 681
《L04 微信小程序从零到发布》
从小程序个人账户申请开始,带你一步步进行开发一个微信小程序,直到提交微信控制台上线发布。
《L03 构架 API 服务器》
你将学到如 RESTFul 设计风格、PostMan 的使用、OAuth 流程,JWT 概念及使用 和 API 开发相关的进阶知识。
最佳答案

你应该看看文档的模型的casts 属性的作用,我们在 order模型中加入了下面这一段代码

protected $casts = [
        'closed'    => 'boolean',
        'reviewed'  => 'boolean',
        'address'   => 'json',
        'ship_data' => 'json',
        'extra'     => 'json',
    ];

自动转换成json格式的,检查一下你的代码

5年前 评论
讨论数量: 2

你应该看看文档的模型的casts 属性的作用,我们在 order模型中加入了下面这一段代码

protected $casts = [
        'closed'    => 'boolean',
        'reviewed'  => 'boolean',
        'address'   => 'json',
        'ship_data' => 'json',
        'extra'     => 'json',
    ];

自动转换成json格式的,检查一下你的代码

5年前 评论

你应该看看文档的模型的casts 属性的作用,我们在 order模型中加入了下面这一段代码

protected $casts = [
        'closed'    => 'boolean',
        'reviewed'  => 'boolean',
        'address'   => 'json',
        'ship_data' => 'json',
        'extra'     => 'json',
    ];

自动转换成json格式的,检查一下你的代码

5年前 评论
circle

@Flourishing 对的,是我将 $casts 打错了,谢谢

5年前 评论

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