是不是应该加个 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
《L05 电商实战》
从零开发一个电商项目,功能包括电商后台、商品 & SKU 管理、购物车、订单管理、支付宝支付、微信支付、订单退款流程、优惠券等
《L02 从零构建论坛系统》
以构建论坛项目 LaraBBS 为线索,展开对 Laravel 框架的全面学习。应用程序架构思路贴近 Laravel 框架的设计哲学。
最佳答案

你应该看看文档的模型的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年前 评论

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