2017-09-01 57 views
0

有以下代碼:我如何掛鉤的方法在laravel分貝

$data = DB::table('blogs') 
     ->where('title', '=', $title); 

現在,我想它已經如上初始化後添加更多的方法。我想在下面添加一些內容,以便將其他位置添加到數據中。

$input = TRUE; 
$data = DB::table('blogs') 
     ->where('title', '=', $title); 

if($input){ 
    $data->where('description','=', $description); //but then, this doesn't work 
} 

我想添加更多,如果就像orderbywhere內。但是,它不起作用。 這樣做的正確方法是什麼?感謝

回答

3

你忘了將添加的條件$數據

試試下面的代碼

$input = TRUE; 
$data = DB::table('blogs') 
     ->where('title', '=', $title); 

if($input){ 
    $data = $data->where('description','=', $description); 
} 

如果需要添加更多的條件:由

$input1 = TRUE; 

if($input1){ 
     $data = $data->where('other_column','=', $other_var); 
    } 

,並在最後抓取詳情

$data = $data->get(); 

希望這會有所幫助。

+0

噢耶..謝謝@meera – smzapp

1

你可以做到這樣的 -

if(your_condition) 
{ 
    $whereCondition = "table.description ='".$description."'"; // for any where condition 
} 
else 
{ 
    $whereCondition = '1=1'; //for no where condition 
} 

$data = DB::table('blogs') 
      ->where('title', '=', $title) 
      ->whereRaw($whereCondition) 
      ->orderBy('table.column','DESC') 
      ->get(); 

希望這能對你有幫助。