Where语句中的函数或变量?

时间:2018-11-14 09:17:56

标签: php mysql laravel

是否可以将函数或变量放在where语句的第一个值中?

例如,

$equipments=DB::table('equipments')
            ->leftjoin('orders', function ($join) {
                $join->on('orders.equipment_id', '=', 'equipments.equipmentID')
                    ->where('orders.orderUser_id','=', Auth::user()->id);
            })
            ->leftjoin('business_users', 'business_users.id', '=' , 'equipments.user_id')
            ->where('equipments.equipmentArchived','=','0')
            *->where($this->countOrders('equipments.equipmentID'),'<','1')*
            ->orderBy('equipments.equipmentListedDate','DESC')
            ->distinct()
            ->paginate(10);

盘点单功能是

public static function countOrders($id)
    {
        //
        $members=DB::table('orders')
            ->leftjoin('users', 'users.id', '=', 'orders.orderUser_id')
            ->leftjoin('equipments', 'equipments.equipmentID', '=', 'orders.equipment_id')
            ->where('orders.equipment_id','=', $id)
            ->where('orders.status','=','ACCEPTED')
            ->count();

        return $members;
    }

1 个答案:

答案 0 :(得分:1)

您可以使用“ whereRaw”方法来实现。

$equipments = DB::table('equipments')
    ->leftjoin('orders', function ($join) {
        $join->on('orders.equipment_id', '=', 'equipments.equipmentID')
            ->where('orders.orderUser_id','=', Auth::user()->id);
    })
    ->leftjoin('business_users', 'business_users.id', '=' , 'equipments.user_id')
    ->where('equipments.equipmentArchived','=','0')
    ->whereRaw('? < ?', [$this->countOrders('equipments.equipmentID'), 1])
    ->orderBy('equipments.equipmentListedDate','DESC')
    ->distinct()
    ->paginate(10);