TP6.0 模型关联条件查询(hasWhere)

 模型层:

public function product(){
     return $this->hasOne("product","nid","product_id");
}

public function storage(){
     return $this->hasOne("storage","nid","storage_id");
}

控制器层:

$product_name=input('product_name');

$StockModel=new StockModel();

$StockModel->hasWhere('product',function($query) use($product_name) {
                        $query->where('product_name','like','%'.$product_name.'%');
                    })->with(['product'=>function($query) use($product_name) {
                        $query->field('product_name,nid');
                    },'storage'=>function($query){
                        $query->field('storage_name,nid');
                    }])->where('stock_code','xxxxx')->select();


版权声明:本文为y_w_x_k原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。