elodev
elodev

Reputation: 343

How do I perform a dynamic query in 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;
    }

Upvotes: 0

Views: 457

Answers (1)

Matteus Hemstr&#246;m
Matteus Hemstr&#246;m

Reputation: 3845

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

Upvotes: 1

Related Questions