0

我是 laravel 的新手。我正在尝试从选择列表中构建查询。我收到以下错误

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;
    }
4

1 回答 1

1

您的表格绝不会发布卫生员牙医或牙科协助Input::get('hygienist')也会如此null。该表单将使用从选择列表中选择的值发布temptype 。也就是说,如果我转到那个表格,选择Hygienist并单击提交,您将拥有.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;
}
于 2013-10-04T07:37:25.300 回答