hasOne(Goods::class, 'id', 'goods_id')->hidden(['content'])->removeOption('soft_delete'); } /** * @notes 关联商品规格模型 * @return \think\model\relation\HasOne * @author 段誉 * @date 2021/7/20 16:35 */ public function goodsItem() { return $this->hasOne(GoodsItem::class, 'id', 'item_id'); } /** * @notes 获取购物车列表 * @param $userId * @return array * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException * @author 段誉 * @date 2021/7/20 16:36 */ public function getCartLists($userId) { return $this->withoutField(['create_time', 'update_time']) ->with([ 'goods' => function ($query) { $query->field(['id', 'name', 'image', 'status', 'delete_time','limit_type','limit_value', 'spec_type', 'delete_time' ]); }, 'goods_item' => function ($query) { $query->field(['id', 'spec_value_str', 'sell_price', 'image', 'stock' ] ); } ]) ->where(['user_id' => $userId]) ->order(['id' => 'desc']) ->select()->toArray(); } /** * @notes 通过规格id获取购物车信息 * @param $itemId * @param $userId * @return array|\think\Model * @author 段誉 * @date 2021/7/20 16:36 */ public function getCartByItem($itemId, $userId) { return $this->with(['goods', 'goods_item']) ->where(['user_id' => $userId, 'item_id' => $itemId]) ->findOrEmpty(); } /** * @notes 通过购物车id获取信息 * @param $id * @param $userId * @return array|\think\Model * @author 段誉 * @date 2021/7/20 16:36 */ public function getCartById($id, $userId) { return $this->with(['goods', 'goods_item']) ->where(['user_id' => $userId, 'id' => $id]) ->findOrEmpty(); } }