if ($request->data == null) { $dia = Carbon::now()->format('Y-m-d'); $notas = NF::leftjoin("n_v_e_s", "n_v_e_s.chave", "n_f_s.key") ->leftjoin("n_f_p_d_v_e_s_s", "n_f_p_d_v_e_s_s.cnpj", "n_v_e_s.cnpj_dest") ->leftjoin("clientes", "clientes.Cliente_ID", "n_f_s.client_id") ->leftjoin("n_f_statuses", "n_f_statuses.id", "n_f_s.status_id") ->leftjoin("n_f_obs", "n_f_obs.id", "n_f_s.obs_status_id") ->leftjoin("routes", "routes.id", "n_f_s.route_id") ->leftjoin("n_f_produtos", "n_f_produtos.nfe_id", "n_v_e_s.id") //->leftjoin("n_f_produtos", "n_f_produtos.nfe_id", "n_f_s.id") //->leftjoin("dataloggers", "dataloggers.MOTORISTA_ID", "routes.driver_id") /* ->groupby( "n_f_s.delivery_date", "clientes.Cliente_Nome", "clientes.Cliente_ID", "n_f_s.nf as NF", "n_f_statuses.name as NF_Status", "n_f_s.horariodata_entrega", "n_f_obs.name", //"dataloggers.TEMPERATURA", "n_f_s.nfphoto", "n_f_s.nfphoto", "n_f_s.id as canhotoid", "n_f_s.status_id", "n_f_s.id as id_nf", "n_v_e_s.chave", "n_f_produtos.nome_produto" ) */ ->where("n_v_e_s.cnpj_dest", $request->session()->get('usuario')) ->whereDate("n_f_s.delivery_date", Carbon::now()->format('Y-m-d')) ->select("delivery_date", "Cliente_Nome", "clientes.Cliente_ID as Cliente", "n_f_s.nf as NF", "n_f_statuses.name as NF_Status", "n_f_s.horariodata_entrega", "n_f_obs.name as Obs_Status", "n_f_s.nfphoto", "n_f_s.status_id", "n_f_s.id as id_nf", "n_v_e_s.chave" ) ->groupby( "n_f_s.delivery_date", "clientes.Cliente_Nome", "clientes.Cliente_ID", "n_f_s.nf", "n_f_statuses.name", "n_f_s.horariodata_entrega", "n_f_obs.name", "n_f_s.nfphoto", "n_f_s.status_id", "n_f_s.id", "n_v_e_s.chave" ) ->when($notaNome, function ($query) use ($notaNome) { return $query->where('n_v_e_s.nf', $notaNome); }) ->get(); }else{ $dia = $request->data; $notas = NF::leftjoin("n_v_e_s", "n_v_e_s.chave", "n_f_s.key") ->leftjoin("n_f_p_d_v_e_s_s", "n_f_p_d_v_e_s_s.cnpj", "n_v_e_s.cnpj_dest") ->join("clientes", "clientes.Cliente_ID", "n_f_s.client_id") ->join("n_f_statuses", "n_f_statuses.id", "n_f_s.status_id") ->leftjoin("n_f_obs", "n_f_obs.id", "n_f_s.obs_status_id") ->leftjoin("routes", "routes.id", "n_f_s.route_id") ->leftjoin("n_f_produtos", "n_f_produtos.nfe_id", "n_v_e_s.id") ->select("delivery_date", "Cliente_Nome", "clientes.Cliente_ID as Cliente", "n_f_s.nf as NF", "n_f_statuses.name as NF_Status", "n_f_s.horariodata_entrega", "n_f_obs.name as Obs_Status", "n_f_s.nfphoto", "n_f_s.status_id", "n_f_s.id as id_nf", "n_v_e_s.chave" ) ->groupby( "n_f_s.delivery_date", "clientes.Cliente_Nome", "clientes.Cliente_ID", "n_f_s.nf", "n_f_statuses.name", "n_f_s.horariodata_entrega", "n_f_obs.name", "n_f_s.nfphoto", "n_f_s.status_id", "n_f_s.id", "n_v_e_s.chave" ) ->where("n_v_e_s.cnpj_dest", $request->session()->get('usuario')) ->when($notaNome, function ($query) use ($notaNome) { return $query->where('n_v_e_s.nf', $notaNome); }) ->when($cliente_id, function ($query) use ($cliente_id) { return $query->where('n_v_e_s.cliente_id', $cliente_id); }) ->when($dia, function ($query) use ($dia) { return $query->whereDate("n_f_s.delivery_date", $dia); }) ->when($produtos, function ($query) use ($produtos) { return $query->where("n_f_produtos.nome_produto", 'LIKE', "%{$produtos}%"); }) ->get(); }