首页 > 其他 > 详细

tp5 商城模型id详情接口

时间:2021-09-11 20:13:01      阅读:50      评论:0      收藏:0      [点我收藏+]

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:效果图

技术分享图片

 

tp5 商城模型id详情接口

原文:https://www.cnblogs.com/xiaoyantongxue/p/15252955.html

踩
(0)
赞
(0)
   
举报
评论 一句话评论(0)
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!