1.建表
(1)商品类型表
(2)商品规格名表
(3)商品属性表
(4)规格值表
2.在公共模块(common)里创建四个模型分别如下:
php think make:model common@Type 商品类型
php think make:model common@Spec 商品规格名
php think make:model common@Attribute 商品属性
php think make:model common@SpecValue 规格值
//商品类型模型
class Type extends Model
{
protected $table='pyg_type';
/**
* 建立类型与规格的关系
*/
public function spec(){
return $this->hasMany(Spec::class,'type_id','id');
}
/**
* 建立类型与属性之间的关系
*/
public function attr(){
return $this->hasMany(Attribute::class,'type_id','id');
}
}
//商品规格模型
class Spec extends Model
{
/**
* 建立规格与规格值的关系
*/
public function specValue(){
return $this->hasMany(SpecValue::class,'spec_id','id');
}
}
3. 在adminapi模块里创建business目录,在目录里创建Type类
namespace app\adminapi\business;
class Type
{
public static function getInfo(){
$info = \app\common\model\Type::with(['spec','attr','Spec.specValue'])->find(16)->toArray();
dd($info);
}
}
4.在adminapi模块里创建Type控制器
php think make:controller adminapi@Type
namespace app\adminapi\controller;
use think\Request;
class Type
{
/**
* 显示资源列表
*
* @return \think\Response
*/
public function index()
{
// $info = \app\common\model\Type::with(['spec','attr','Spec.specValue'])->find(16)->toArray();
// dd($info);
return \app\adminapi\business\Type::getInfo();
}
5.设置资源路由
Route::resource('type','type');