Muhammad Hamza Younas
Muhammad Hamza Younas

Reputation: 508

laravel - custom search not working, multiple fields

i am trying to create search for my dashbord where have 4 field,

so i can search one by one or both at the same time name, age, location, phone, i wrote a code but its not working

    $q = Model::query();

  if (Input::has('name'))
  {
    $q->where('name',Input::get('name'));
  }

  if (Input::has('age'))
  {
    $q->where('age',Input::get('age'));
  }

  if (Input::has('location'))
  {
    $q->where('location', Input::get('location'));
  }

  if (Input::has('phone'))
  {
    $q->where('phone', Input::get('phone'));
  }

Getting empty output:

[]

Upvotes: 0

Views: 434

Answers (1)

user7747472
user7747472

Reputation: 1952

Try this,

 $q = Model::select('id','name','age'); //What ever field you need, This will create model instance

  if (Input::has('name') && !empty(Input::get('name'))
  {
    $q->where('name',Input::get('name'));
  }

  if (Input::has('age') && !empty(Input::get('age'))
  {
    $q->where('age',Input::get('age'));
  }

  if (Input::has('location') && !empty(Input::get('location'))
  {
    $q->where('location', Input::get('location'));
  }

  if (Input::has('phone') && !empty(Input::get('phone'))
  {
    $q->where('phone', Input::get('phone'));
  }
$result = $q->get()->toArray(); 
print_r($result);

This should give you what you are looking for

Upvotes: 2

Related Questions