ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
# 软删除和追加字段 ``` ~~~ protected $name ='web_page_single'; use SoftDelete; protected $defaultSoftDelete = null; protected $append = [ 'status_text' ]; // 状态 const IS_STATUS = [ '2' => '关闭', '1' => '开启', ]; ~~~ ``` # 设定属性 获取和设定 ``` ~~~ public function getStatusTextAttr($value, $data): string { if (!isset($data['status'])) return ''; return self::IS_STATUS[$data['status']] ?? $data['status']; } ~~~ public function getStatusTextAttr($value, $data): string { if (!isset($data['status'])) return ''; return self::IS_STATUS[$data['status']] ?? $data['status']; } public function getTypeTextAttr($value, $data): string { if (!isset($data['type'])) return ''; return self::IS_TYPE[$data['type']] ?? $data['type']; } ------------------------------------------------------------------------ ~~~ protected function setShowTimeAttr($value) { if($value != 0){ return strtotime($value); }else{ return $value; } } ~~~ ``` # 模型关联查询数据 ~~~ $query = (new UserCommissionLog)->alias('c')->where(['customer_id' => config('sys.platform_id')]) // 收益类型 金额 账户余额 粉丝手机号 时间 来源 状态 应用类型 ->field('c.id, c.user_id, c.user_grade, c.amount, c.after_amount, c.create_time, c.order_type, c.status, c.app_type') ->whereIn('c.user_id', $user_ids) ->when(key_exists('status', $params), function (Query $query) use ($params) { $query->where(['c.status' => $params['status']]); }) ->when(key_exists('user_grade', $params), function (Query $query) use ($params) { $query->where(['c.user_grade' => $params['user_grade']]); }) ->when(key_exists('create_time', $params), function (Query $query) use ($params) { $tmpTime = explode(' - ', $params['create_time']); $query->whereBetween('c.create_time', [@$tmpTime[0], @$tmpTime[1]]); }) ->when(key_exists('mobile', $params), function (Query $query) use ($params) { $query->leftJoin('user u', 'u.id=c.user_id')->where(['u.mobile' => $params['mobile']]); }) ->with(['user' => function(Query $query) { $query->field('id, mobile'); }]) ->order('c.id desc')->paginate($this->limit); ~~~ # 模型关联 ``` ~~~ $data = $this->model->where(['id' => $id]) ->field('id, order_no, status, user_id, create_time, pay_time, pay_type, deliver_time, express_no, username, mobile, province, city, area, location, remarks, service_remarks') ->where(['customer_id' => config('sys.platform_id')]) ->with(['user' => function (Query $query) { $query->field('id, nickname, mobile'); }, 'order_details' => function (Query $query) { $query->field('goods_order_id, goods_image, goods_name, goods_code, goods_price, goods_num, goods_total_price, discount_price, freight_price, goods_total_price, real_price'); }]) ->find(); ~~~ ~~~ public function user(): HasOne { return $this->hasOne(User::class, 'id', 'user_id'); } public function orderDetails(): HasMany { return $this->hasMany(GoodsOrderDetail::class, 'goods_order_id'); } ~~~ ```