dqzow3859 2014-06-30 17:28
浏览 86
已采纳

Laravel在Controller where子句中的条件

I'm trying to build a query based on URL parameters. When the Controller is loaded I need to check which parameters have been provided and build a query from them. It's working with static values, but isn't working with conditional statements. Is my laravel syntax correct?

class OrdenesController extends BaseController {

public function showOrdenes($action)
{
  $my_id = Auth::user()->id;
  $my_cod = Auth::user()->codprov;

  switch ($action) 
  {
    case 'list':
      $rows = DB::table('ordens')->count();
      if(Input::get("jtSorting"))
      {
       $search = explode(" ", Input::get("jtSorting"));            
       $numorden= Input::get("nro_orden");
       $filtros =explode(" ", $filtros);

       $data = DB::table("ordens")
        ->select(array('*', DB::raw('SUM(cant_pend) as cant_pend'), DB::raw('SUM(importe) as importe')))
        ->where('cod_prov', '=', $my_cod)

        ->where('nro_orden', '=', $numorden)///work

        ---------- ////no work
        if (Input::has('nro_orden')) {
           ->where('nro_orden', '=', $numorden)
        }
        ---------- /// no work

        ->groupBy('nro_orden')
        ->skip(Input::get("jtStartIndex"))
        ->take(Input::get("jtPageSize"))
        ->orderBy($search[0], $search[1])
        ->get();
      }
      return Response::json(
        array(
          "Result"      =>    "OK",
          "TotalRecordCount"  =>    $rows,
          "Records"     =>    $data
        )
      );
      break;

   };  
  }    
}
  • 写回答

1条回答 默认 最新

  • duanlao6573 2014-06-30 18:10
    关注

    You are missing the variables, no? You haven't told PHP what variable/object to do the where() to in your condition. The magic of Laravel's Eloquent (and a lot of other libraries) is that when you call its methods, it returns itself (the object) back so you can make another method call to it right away.

    So when you do this:

    $data = DB::table("ordens")
        ->select(...)
        ->where(...);
    

    is the same as:

    $data = DB::table("ordens");
    $data = $data->select(...);
    $data = $data->where(...);
    

    But you are trying to do ->where(...) right away after if condition. You need to tell PHP which object/variable you are trying to call the method from. Like this:

    $num = Input::get("nro_orden");
    
    $data = DB::table("ordens")
        ->select(array('*', DB::raw('SUM(cant_pend) as cant_pend'), DB::raw('SUM(importe) as importe')))
        ->where('cod_prov', '=', $my_cod);
    
    if (Input::has('nro_orden')) {
        $data = $data->where('nro_orden', '=', $num);
    }
    
    $data = $data->groupBy('nro_orden')
        ->skip(Input::get("jtStartIndex"))
        ->take(Input::get("jtPageSize"))
        ->orderBy($search[0], $search[1])
        ->get();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 爬取1-112页所有帖子的标题但是12页后要登录后才能 我使用selenium模拟登录 账号密码输入后 会报错 不知道怎么弄了
  • ¥30 关于用python写支付宝扫码付异步通知收不到的问题
  • ¥50 vue组件中无法正确接收并处理axios请求
  • ¥15 隐藏系统界面pdf的打印、下载按钮
  • ¥15 MATLAB联合adams仿真卡死如何解决(代码模型无问题)
  • ¥15 基于pso参数优化的LightGBM分类模型
  • ¥15 安装Paddleocr时报错无法解决
  • ¥15 python中transformers可以正常下载,但是没有办法使用pipeline
  • ¥50 分布式追踪trace异常问题
  • ¥15 人在外地出差,速帮一点点