本文介绍了如何在 Laravel 中查询的 WHERE 子句中使用正则表达式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个名为Shows"的表.有一列show_date".我想检索 show_date 为今天日期的节目.
以下是我的查询
$s = DB::table('shows')->get();$a = DB::table('shows')->select('show_date')->get();foreach ($s as $key => $value){$date_test = date('Y-m-d');$s_test = DB::table('shows')->where('show_date',preg_grep('/"'.$value->show_date.'"./', $a->show_date))->get();echo "
";var_dump($s_test);if(explode(" ",$value->show_date)[0] == date('Y-m-d')){$shows1 = DB::table('shows')->where('id',$value->id)->get();$s1 = DB::table('交易')->select(DB::raw("GROUP_CONCAT(selected_seats SEPARATOR '') as selected_seats"),'userid','amount','show_id')-> where("show_id","=",$value->id)->groupBy('userid')-> groupBy('金额')-> orderBy('userid','ASC')-> orderBy('金额', 'DESC')->get();if($s1 != null){echo $value->id;$c = 计数($s1);$sub_count1 = 0;$next_id = "";$total_array = 0;for($i=0;$i<$c;$i++){$first_character = $s1[$i]->selected_seats;$sub_count = substr_count($s1[$i]->selected_seats, ',');$sub_count1 = $sub_count1 + $sub_count;//得到总数.座位数for($j=0,$k=0;$j<$sub_count;$j++,$k++){//用逗号分割字符串.$s = expand(',',$first_character);//获取总数表中一行中列出的座位名称.例如 A 1,B 2.然后 $sub_count 将是 2$p = $s[$j][0];}}//从交易表中获取每个节目的座位.$demo = DB::table('theater_setting')->select('row_seats_ sell_price','row')->where('show_id',$value->id)->get();foreach ($demo as $key => $val) {$categoryArr[$val->row]=$val->row_seats_ sell_price;}$demo4 = DB::table('theater_setting')->select('row_seats_ sell_price','row')->where('show_id',$value->id)->get();$demo3 = DB::table('transactions')->where('show_id',$value->id)->select('selected_seats','userid')->get();for($p=0;$p
selected_seats,0,-1));$trans[] = $demo3[$p]->userid;foreach ($arr as $k => $v){$seats[$demo3[$p]->userid][]=$v;}}foreach ($seats as $user_id=>$v){for ($h=0; $h < count($v); $h++){$e = expand(" ", $v[$h]);$p = $e[0];$demo_array[$p][$user_id][] = $v[$h];}$users = DB::table('users')->where('id',$user_id)->get();}return view('Backend.NewReportByShowsCategory2')->with(['s1'=>$s1,'shows1'=>$shows1,'demo'=>$demo,'categoryArr'=>$categoryArr,'demo3'=>$demo3,'demo4'=>$demo4,'demo_array'=>$demo_array]);}别的{return view('Backend.NewReportByShowsCategory2')->with(['s1'=>$s1]);}} }
我收到以下错误:
类 stdClass 的对象无法转换为字符串
解决方案您可以将 show_date 转换为 DATE 并将其与当前日期进行比较 -
$s_test = DB::table('shows')->whereRaw('DATE(show_date)=CURRENT_DATE')->get()
但是,这是用于选择具有特定日期(在本例中为当前日期)的行的正则表达式查询,
DB::table('shows')->whereRaw("show_date REGEXP'".Carbon\Carbon::now()->toDateString()."'")->get()
I have a table named "Shows". There is a column "show_date". I want to retrieve the shows whose show_date is todays date.
Following is my query
$s = DB::table('shows')->get(); $a = DB::table('shows')->select('show_date')->get(); foreach ($s as $key => $value) { $date_test = date('Y-m-d'); $s_test = DB::table('shows')->where('show_date',preg_grep('/"'.$value->show_date.'"./', $a->show_date))->get(); echo "<pre>"; var_dump($s_test); if(explode(" ",$value->show_date)[0] == date('Y-m-d')) { $shows1 = DB::table('shows')->where('id',$value->id)->get(); $s1 = DB::table('transactions') ->select(DB::raw("GROUP_CONCAT(selected_seats SEPARATOR '') as selected_seats"),'userid','amount','show_id') ->where("show_id","=",$value->id) ->groupBy('userid') ->groupBy('amount') ->orderBy('userid','ASC') ->orderBy('amount', 'DESC') ->get(); if($s1 != null) { echo $value->id; $c = count($s1); $sub_count1 = 0; $next_id = ""; $total_array = 0; for($i=0;$i<$c;$i++) { $first_character = $s1[$i]->selected_seats; $sub_count = substr_count($s1[$i]->selected_seats, ','); $sub_count1 = $sub_count1 + $sub_count;//to get the total no. of seats for($j=0,$k=0;$j<$sub_count;$j++,$k++) { // split the string with comma. $s = explode(',',$first_character); // get total no. of seat names listed in one row in table.eg A 1,B 2. Then $sub_count would be 2 $p = $s[$j][0]; } } // get seats for each show from transaction table. $demo = DB::table('theater_setting')->select('row_seats_selling_price','row')->where('show_id',$value->id)->get(); foreach ($demo as $key => $val) { $categoryArr[$val->row]=$val->row_seats_selling_price; } $demo4 = DB::table('theater_setting')->select('row_seats_selling_price','row')->where('show_id',$value->id)->get(); $demo3 = DB::table('transactions')->where('show_id',$value->id)->select('selected_seats','userid')->get(); for($p=0;$p<count($demo3);$p++) { $arr = explode(',', substr($demo3[$p]->selected_seats,0,-1)); $trans[] = $demo3[$p]->userid; foreach ($arr as $k => $v) { $seats[$demo3[$p]->userid][]=$v; } } foreach ($seats as $user_id=>$v) { for ($h=0; $h < count($v); $h++) { $e = explode(" ", $v[$h]); $p = $e[0]; $demo_array[$p][$user_id][] = $v[$h]; } $users = DB::table('users')->where('id',$user_id)->get(); } return view('Backend.NewReportByShowsCategory2')->with([ 's1'=>$s1,'shows1'=>$shows1,'demo'=>$demo,'categoryArr'=>$categoryArr,'demo3'=>$demo3,'demo4'=>$demo4,'demo_array'=>$demo_array]); } else { return view('Backend.NewReportByShowsCategory2')->with([ 's1'=>$s1]); } }
}
I am getting the following error:
Object of class stdClass could not be converted to string
解决方案You could convert show_date to a DATE and compare it with the current date -
$s_test = DB::table('shows')->whereRaw('DATE(show_date)=CURRENT_DATE')->get()
However, here's the regex query for selecting rows with a particular date (the current date in this case),
DB::table('shows')->whereRaw("show_date REGEXP '". Carbon\Carbon::now()->toDateString() . "'")->get()
这篇关于如何在 Laravel 中查询的 WHERE 子句中使用正则表达式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!