的结果
{#457 ▼
+"id": 14
+"amount": "110092"
+"discount": 0
+"images": "{"images":{"original":"\/uploads\/post_images\/2017\/1513070310.jpeg","300":"\/uploads\/post_images\/2017\/300_1513070310.jpeg","600":"\/uploads\/post_images\/2017\/600_1513070310.jpeg","900":"\/uploads\/post_images\/2017\/900_1513070310.jpeg"},"thumbnail":"\/uploads\/post_images\/2017\/300_1513070310.jpeg"} ◀"
+"created_at": "2017-12-28 17:48:48"
+"updated_at": "2017-12-28 19:24:18"
}
查询,我试图从thumbnail
为images
的图片列中获取JsonObject
,但我收到此错误:
"Illegal string offset 'thumbnail'"
我的代码:
$userShoppings = \DB::table('payments as s')
->join('products as p', 's.product_id', '=', 'p.id')
->select('p.*', 's.*',\DB::raw('s.ordering_count * s.price as cost'))
->whereUserId(auth()->user()->id)
->first();
dd($userShoppings->images['thumbnail']);
或
dd($userShoppings->images['images']['thumbnail']);
错误:
"Illegal string offset 'images'"
我的模特:
class Shopping extends Model
{
protected $fillable = [];
protected $casts=[
'images'=>'array'
];
}
class Payment extends Model
{
protected $fillable = [];
protected $casts=[
'images'=>'array'
];
}
答案 0 :(得分:1)
您需要将$userShoppings->images
转换为json或数组才能使用它
答案 1 :(得分:0)
它是一个JSON对象,请尝试:
$userShoppings->images.thumbnail