duaj39673 2013-10-04 05:26
浏览 66

如何在Laravel 4中执行动态查询

I am new to laravel. I am trying to build a query from a select list. I am getting the following error

strtolower() expects parameter 1 to be string

Here is my form

        <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>

Here is my route

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


    $temp = User::getTemps();

    return $temp;


});

Here is the method in my user model...

 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条回答 默认 最新

  • dongliao6777 2013-10-04 07:37
    关注

    Your form will never post hygienist, dentist or dentalassistance. So Input::get('hygienist') will be null. The form will post temptype with the selected value from the select list. That is, if I go to that form, select Hygienist and click submit, you will have Input::get('temptype') = "Hygienist".

    So you should change getTemps to:

    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;
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看