【问题标题】:How to use regular expression in the WHERE clause of query in Laravel?如何在 Laravel 查询的 WHERE 子句中使用正则表达式?
【发布时间】:2016-10-26 17:36:53
【问题描述】:

我有一个名为“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 "<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]);
  }

}

}

我收到以下错误:

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

【问题讨论】:

  • 什么是 $value->show_date?我的猜测是它是一个 Carbon 对象,所以你需要从中获取日期作为字符串
  • @rypskar,是的,这是我访问该列的对象。
  • 您是否测试过 $value->show_date->toDateString() 而不是您的正则表达式?或者你可以尝试类似 ->where('show_date', Carbon::today())->get();

标签: php regex laravel where-clause


【解决方案1】:

还有另一种方法:

DB::table('shows')->where('show_date', 'REGEXP',  Carbon\Carbon::now()->toDateString())->get();

【讨论】:

    【解决方案2】:

    您可以将 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() 
    

    【讨论】:

    • 不需要使用 raw,只要 where('show_date', 'REGEXP', 'somedate)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-18
    • 1970-01-01
    • 2015-07-26
    • 2020-09-20
    • 1970-01-01
    • 2019-08-20
    相关资源
    最近更新 更多