2016-06-24 2 views
0

У меня есть таблица под названием «Показывает». Существует столбец «show_date». Я хочу получить шоу, чья show_date является текущей датой.Как использовать регулярное выражение в предложении WHERE запроса в Laravel?

Ниже мой запрос

$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 не может быть преобразован в строку

+0

Что такое $ ценностно> show_date? Я предполагаю, что это объект Carbon, поэтому вам нужно получить дату как строку из него – rypskar

+0

@rypskar, да, это объект, через который я обращаюсь к столбцу. – prajakta

+0

Вы тестировали $ value-> show_date-> toDateString() вместо своего регулярного выражения? Или вы можете попробовать что-то вроде -> где ('show_date', Carbon :: today()) -> get(); – rypskar

ответ

4

Вы можете конвертировать show_date в a 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() 
+0

Thanku Rahul ... Это сработало отлично .. :) – prajakta

2

Существует альтернативный способ:

DB::table('shows')->where('show_date', 'REGEXP', Carbon\Carbon::now()->toDateString())->get(); 
Смежные вопросы