Question

I want to create a function that will search all the records within a table from a single search input. I have done a simple search which gets the value from input and within a foreach loop i am receiving the array of results related to the search.

Controller function

public function getSearch(){
    $input = Input::get('search');

    $validator = Validator::make($input, array(
            'search' => 'required'
        )
    );

    if($validator->fails()){
        die('error');
    } else {
      $search_terms = explode(' ', $input);

      foreach($search_terms as $term)
      {
          $query = Upload::where('filename', 'LIKE', '%'.$term.'%');
      }

      $results = $query->get();
      die($results);
    }
}

In validator I have only one argument to make it required and passed within the array.

'search' => 'required'

I assume that the reason i am getting this error is because I am only passing one argument in validator's array?

ErrorException
Argument 1 passed to Illuminate\Validation\Factory::make() must be of the type array, string given, called in /www/assetlibr/vendor/laravel/framework/src/Illuminate/Support/Facades/Facade.php on line 211 and defined

Update: Because the validator is not the best practice here, instead I removed the validator and added an if statement like

if(Input::has('search'))

and is now working perfectly!

Was it helpful?

Solution

the first argument passed to make need to be array. you give string..

look at the docs: http://laravel.com/docs/validation

change your code to this:

    $serach = Input::get('search');

    $validator = Validator::make(
                array(
            'search' => $search,
                ), 
                array(
            'search' => 'required'
                )
     );

OTHER TIPS

Validator requires two arrays, one for data and other one for validation rules, so yes, you are missing it there in Validator::make() call. Here is how you should perform the validation:

$input = array('search' => Input::get('search'));

$validator = Validator::make($input, array(
        'search' => 'required'
    )
);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top