Php 如何在Laravel查询的WHERE子句中使用正则表达式?

Php 如何在Laravel查询的WHERE子句中使用正则表达式?,php,regex,laravel,where-clause,Php,Regex,Laravel,Where Clause,我有一张名为“Shows”的桌子。有一列“显示日期”。我要检索其show_date为todays 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 = D

我有一张名为“Shows”的桌子。有一列“显示日期”。我要检索其show_date为todays 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 "<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]);
  }

}
$s=DB::table('shows')->get();
$a=DB::table('shows')->select('show_date')->get();
foreach($s作为$key=>$value)
{
$date_test=日期('Y-m-d');
$s_test=DB::table('shows')->其中('show_date',preg_grep('/'。$value->show_date'/',$a->show_date))->get();
echo”“;var_dump($s_test);
如果(分解(“,$value->show_date)[0]==date('Y-m-d'))
{
$shows1=DB::table('shows')->where('id',$value->id)->get();
$s1=DB::表('transactions')
->选择(DB::raw(“组集合(所选席位分隔符“”)作为所选席位”)、'userid'、'amount'、'show_id')
->其中(“show_id”,“=”,$value->id)
->groupBy('userid')
->分组依据(“金额”)
->orderBy('userid','ASC')
->orderBy('amount','DESC')
->get();
如果($s1!=null)
{
echo$value->id;
$c=计数($s1);
$sub_count1=0;$next_id=“”;$total_array=0;
对于($i=0;$i已选席位;
$sub_count=substr_count($s1[$i]>选定的_座位,,);
$sub_count1=$sub_count1+$sub_count;//获取总座位数
对于($j=0,$k=0;$jselect('row\u seats\u salling\u price','row')->where('show\u id',$value->id)->get();
foreach($key=>$val){
$categoryArr[$val->row]=$val->row\u座位\u售价;
}
$demo4=DB::table('theater\u setting')->select('row\u seats\u salking\u price','row')->where('show\u id',$value->id)->get();
$demo3=DB::table('transactions')->where('show_id',$value->id)->select('selected_seats','userid')->get();
对于($p=0;$PSU当选席位,0,-1));
$trans[]=$demo3[$p]->userid;
foreach($arr为$k=>$v)
{ 
$seats[$demo3[$p]->userid][]=$v;
}
}
foreach($user\u id=>$v的座位)
{  
对于($h=0;$hwhere('id',$user_id)->get();
}   
返回视图('Backend.NewReportByShowsCategory2')->带有([
“s1'=>s1美元,'shows1'=>shows1美元,'demo'=>demo美元,'categoryArr'=>categoryArr美元,'demo3'=>demo3美元,'demo4'=>demo4美元,'demo_数组'=>demo_数组]);
}
其他的
{
返回视图('Backend.NewReportByShowsCategory2')->带有([
's1'=>s1美元];
}
}
}

我得到以下错误:


类stdClass的对象无法转换为字符串

您可以将show_date转换为日期,并将其与当前日期进行比较-

DB::table('shows')->whereRaw("show_date REGEXP '". Carbon\Carbon::now()->toDateString() . "'")->get() 
但是,这里有一个用于选择具有特定日期(本例中为当前日期)的行的正则表达式查询


还有另一种方法:


什么是$value->show_date?我猜它是一个碳元素对象,所以需要从中获取字符串形式的日期it@rypskar,是的,它是我访问列的对象。您是否测试了$value->show_date->toDateString()而不是regexp?或者您可以尝试类似的方法->where('show_date',Carbon::today())->get();不需要使用raw,只需在哪里('show\u date',REGEXP','somedate)
DB::table('shows')->whereRaw("show_date REGEXP '". Carbon\Carbon::now()->toDateString() . "'")->get() 
DB::table('shows')->where('show_date', 'REGEXP',  Carbon\Carbon::now()->toDateString())->get();