0

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:

[]
1
  • Maybe you should include the whole code. This is output of what? What's your Laravel version? Commented Jun 16, 2018 at 18:24

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

4 Comments

why toArray()?
toArray() is to get response in array format. If you dont give that you will get response as an object.
user774742 i tried your code its working but problem only first field is working when i leave first two fields empty or leave just first empty getting this error Illuminate\Database\Eloquent\Collection Object ( [items:protected] => Array ( ) ) []
Dont use array, And i made a mistake before . I forgot to add && in the condition please check on that one

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.