You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Laravel中Call to undefined function where()错误求助

解决Laravel中「Call to undefined function where()」错误

嘿,这个问题其实是个非常容易疏忽的小笔误!你看报错的那行代码:

-where('Commande_nadine.DATE_DOCUMENT_CMD_ACHAT_FRS','>',$Debut)

这里的链式调用符号写错了——Laravel查询构建器的方法调用需要用->(减号加大于号),但你只写了-(单个减号),PHP会把-where当成一个不存在的函数去执行,自然就抛出「Call to undefined function where()」的错误了。

修正后的完整代码

只需要把那行的-where改成->where就可以解决问题,修正后的代码如下:

public function Calculer( Request $request ) {
    if(isset($request)) {
        $marque = $request->get('marque');
        $modele = $request->get('modele');
        $finition = $request->get('finition');
        $Debut = $request->get('Debut');
        $Fin =$request ->get('Fin');
        $Debut=strtotime($Debut);
        $Fin=strtotime($Fin);
        $TotalAproduire = 100;
        $Commande = DB::connection('sqlsrv2')->table('Commande_nadine')
            ->join('finition','Commande_nadine.CodeFinition','=','finition.CodeFinition')
            ->where('finition.CodeFinition','=',$finition)
            ->where('finition.CodeModele','=',$modele)
            ->where('Commande_nadine.DATE_DOCUMENT_CMD_ACHAT_FRS','>',$Debut)
            ->where('Commande_nadine.DATE_DOCUMENT_CMD_ACHAT_FRS','<',$Fin)
            ->where('finition.CodeMarque','=',$marque)
            ->count('Commande_nadine.RECID_NADIN');
        $PourcentageCommandee=($Commande/$TotalAproduire)*1000;
        return $PourcentageCommandee ;
    } else {
        echo "failed";
    }
}

额外小建议

  • 可以给你的IDE开启PHP语法检查功能,这类语法笔误在编写代码时就能被及时提示,避免后续排查问题的麻烦;
  • 如果你有多个同类型的where条件,可以把它们合并成数组形式,让代码更简洁,比如:
->where([
    ['finition.CodeFinition', '=', $finition],
    ['finition.CodeModele', '=', $modele],
    ['finition.CodeMarque', '=', $marque],
])

内容的提问来源于stack exchange,提问作者M_M

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.11 09:04:08