laravel with 关联后 model 里面方法的调用

$qry = PurchaseRep::query();
$qry->where('type', 0)->where('ec_id', 1);#type  随意一个 0,1 或2

if ($req->sku) {
    $qry->where('sku', $req->sku);
}
if ($req->fnsku) {
    $qry->where('fnsku', $req->fnsku);
}

$qry->with(['product' => function($qry){
        $qry->select('SmallImage','id','sku', 'PackageDimensions')->where('ec_id', 1);
    }]);
$qry->with('inventoryReport');

$res = $qry->paginate( $req->per_page ?: self::$per_page );
$dat = $res->items();
foreach ($dat as &$v) {
    
if($product->first()) {#如果有商品
    //@dev wbz
    #计算仓租 = 体积(单位:feet) * 仓租费率
    $volume =  $product->first()->getPackageVolume('feet');  //product  model 里面定义的方法
    $af = [10, 11, 12];  // 10-12 月份
    if ($product->first()->isOversize()) {  //product model 里面定义的方法

        if ( in_array(date('n'), $af)) {
            $v->warehouse_fee = $volume * $oversize_af_10;
        } else {
            $v->warehouse_fee = $volume * $oversize_bf_9;
        }
    } else {

        if ( in_array(date('n'), $af)) {
            $v->warehouse_fee = $volume * $standard_af_10;
        } else {
            $v->warehouse_fee = $volume * $standard_bf_9;
        }
    }

    $v->warehouse_fee = round($v->warehouse_fee, 2);

}
}

 

 

Model:  whereRaw 方法

### scope作用域

public function scopeSelectPaymentStatus ($query, $status=0)
{
       if ($status) {
           return $query->whereRaw('payment_amount = product_amount+fee_amount');

       } else {
           return $query->whereRaw('payment_amount != product_amount+fee_amount');

       }
}

使用方法:

$res =  $Model->where()

->selectPaymentStatus()

->get();

 

 

 

你可能感兴趣的:(php)