tp5 商城模型id详情接口
2024-09-16 03:21:17
1:创建模型
2:定义关联模型
<?php namespace app\common\model; use think\Model;
use traits\model\SoftDelete;
class TypeModel extends Model
{
use SoftDelete;
//链接pyg商品类型表
protected $table = 'pyg_type';// 进行和规格模型关联,1对多
public function spec()
{
return $this->hasMany('Spec', 'type_id', 'id');
}
// 进行和属性模型进行关联
public function attribute()
{
return $this->hasMany('Attribute', 'type_id', 'id'); }
3:定义规格名称和规格值 规格名和规格值一对多
<?php namespace app\common\model; use think\Model;
use traits\model\SoftDelete;
class Spec extends Model
{
//
use SoftDelete;
protected $table = 'pyg_spec';
//关联规格和规格值 一个规格对应多个规格值
public function specValue()
{
return $this->hasMany('SpecValue', 'spec_id', 'id');
}
4:控制器代码:
public function read($id)
{
//商品模型详情
// 验证参数
if (!intval($id)) {
return json(['code' => 500, 'message' => 'id参数不正确']);
}
// 数据库处理
$res = TypeModel::with('spec,attribute,spec.specValue')->find(16);
// print_r(collection($res)->toArray());
return json(['code' => 200, 'message' => '商品详情展示', 'data' => $res]);
}
5:路由:
//商品模型列表展示
Route::get('index','task/type/index');
6:效果图
最新文章
- #英文#品读中国城市个性——上海人的精明&;头啖汤
- Tomcat 7.0的配置
- 前端XSS攻击和防御
- poj 3468:A Simple Problem with Integers(线段树,区间修改求和)
- c/c++小知识
- POJ 2195 D - Going Home 费用流
- Java环境变量批处理文件
- 从零开始Unity3D游戏开发【3烘焙】
- POJ输出状态的逻辑。
- XJOI1595空中楼阁【最短路】
- linux卸载openjdk
- Alpha 冲刺 (3/10)
- redis主从复制详述
- P1006 传纸条-洛谷luogu-dp动态规划
- BZOJ1567 [JSOI2008]Blue Mary的战役地图 二分答案 哈希
- 系统windows版本修改
- Oracle 字符集更改
- tidb 升级步骤
- 【拓扑排序】BZOJ4010-[HNOI2015]菜肴制作
- python的学习笔记之——time模块常用内置函数