model设置
public function orderGoods(){
return $this->hasMany(OrderGoodsModel::class,'orderId','id');
}
控制器设置
//单表链接
$grid->column('id', __('Id'))->expand(function ($model){
$goods = $model ->orderGoods()->get()->map(function ($orderGoods){
return $orderGoods->only(['id']);
});
return new Table(['id'],$goods->toArray());
});
//多表链接
$grid->column('id', __('Id'))->expand(function ($model){
$goods = $model ->orderGoods()->with('product_type','product_brand')->get()->map(function ($orderGoods){
return [
'id' => $orderGoods->id,
'goods_type' => $orderGoods->getRelation('product_type')->name,
'goods_brand' => $orderGoods->getRelation('product_brand')->name,
'goodName' => $orderGoods->goodName,
'goodsNums' => $orderGoods->goodsNums,
'total_prices' => $orderGoods->total_prices
];
});
return new Table(['id','商品分类','商品品牌','商品名称','数量','商品价格'],$goods->toArray());
});
//引用
use Encore\Admin\Widgets\Table;
//注:orderModel.php
public function orderGoods(){
return $this->hasMany(OrderGoodsModel::class,'orderId','id');
}
OrderGoodsModel
public function product_type(){
return $this->hasOne(ProductTypeModel::class,'id','prot_id');
}
public function product_brand(){
return $this->hasOne(ProductbrandModel::class,'id','goods_brand');
}