/** * 根据商品id 获取 商品详情 * 商品头图 product 商品详情图 productImage 商品参数 productProperty * productImage关联了image表 productImage中 order字段 定义商品详情多张图片的排序 */ $product = self::with(['imgs.imgUrl','properties']) ->find($id) ;
以上链式中 imgs 是 product->productImage 1->hasmany
imgUrl 是 productImage->image 1->belongsTo
properties 是 product->productProperty 1->hasmany
productImage 表中 order 字段 是 各个图片的展示顺序
with() 链式表达式中可以传 字符串 with('img')
可以传数组with(['imgs.imgUrl','properties'])
可以传闭包函数
//img关联imgUrl 对此通过order字段排序之后 再关联properties $product = self::with([ 'imgs' => function($query) { $query->with('imgUrl') ->order('order','asc') ; } ]) ->with(['properties']) ->find($id) ;