2013-10-04 72 views
0

我是laravel的新手。我正試圖從選擇列表中建立一個查詢。我收到以下錯誤如何在Laravel 4中執行動態查詢

用strtolower()預計參數1是字符串

這裏是我的形式

 <form action="search" method="post" accept-charset="utf-8"> 
       <div class="form-group"> 

        <select name="temptype" class="form-control"> 
         <option value="" selected="selected">Select Temp Type</option> 
         <option value="hygienist" >Hygienist</option> 
         <option value="dentist" >Dentist</option> 
         <option value="dentalassistant" >Dental Assistant</option> 
        </select> 

       </div><!-- end username form group --> 
     </div><!-- end .modal-body --> 
     <div class="modal-footer"> 
      <input type="submit" name="submit" class="btn btn-primary" /> 
     </div> 
     </form> 

這裏是我的路線

Route::post('search', function(){ 


    $temp = User::getTemps(); 

    return $temp; 


}); 

這裏是我用戶模型中的方法...

public static function getTemps() 
    { 

     $type = array (

      'hygienist' => Input::get('hygienist'), 
      'dentist' => Input::get('dentist'), 
      'dentalassistance' =>Input::get('dentalassistance') 

     ); 


     $temps = DB::table('users') 
      ->select('usertype', $type) <---- I think this has to be a string but my question is how do I make this dynamic... How do I pass the string value from what the user selects and pass it into the query?> 
      ->get(); 

     return $temps; 
    } 

回答

1

您的形式永遠不會發布衛生員牙醫dentalassistance。所以Input::get('hygienist')將是null。表格將從選擇列表中發佈temptype與選定的。也就是說,如果我轉到該表格,請選擇衛生員並單擊提交,您將有Input::get('temptype') = "Hygienist"

所以,你應該改變getTemps到:

public static function getTemps() 
{ 
    // Create a array of allowed types. 
    $types = array('hygienist', 'dentist', 'dentalassistance'); 

    // Get what type the user selected. 
    $type = Input::get('temptype'); 

    // Make sure it is a valid type. 
    if(!in_array($type, $types)) 
    { 
     return App::abort(500, "Invaild temptype."); 
    } 



    $temps = DB::table('users') 
     ->select('usertype', $type) 
     ->get(); 

    return $temps; 
} 
+0

謝謝這個結果不錯。這是它現在正在工作的一個愚蠢的錯誤。 – elodev