0

I have four input fields : Like this

$cou = Input::get('activitycou');
$type = Input::get('activitytype');
$Des = Input::get('activityDes');

I do the following :

If ($type!=null){
$query=Activity::where('id', $type)->get();   
}
If ($Des!=null){
$query=Activity::where('des_id', $Des)->get();   
}

etc However the problem is that $query is not displayed correctly can someone suggest a solution.

8
  • I need the query for my post view.So it needs to be query at every if. Commented Jul 4, 2016 at 18:16
  • 1
    Is it throwing any error? Commented Jul 4, 2016 at 18:17
  • no but the query is connected and gives me combined results what I want is each query a result maybe another way than if Commented Jul 4, 2016 at 18:20
  • how should i approach this? Commented Jul 4, 2016 at 18:21
  • 1
    have a look at: stackoverflow.com/questions/14179758/… Commented Jul 4, 2016 at 19:48

1 Answer 1

1

If you want to get the result combined with all of your options you can do the follow (with union method you can combine multiple queries):

If ($type!=null && $Des!=null && $cou!=null){
   $query1 = Activity::where('id', $type);   
   $query2 = Activity::where('des_id', $Des);
   $query3 = Activity::where('des_id', $cou);
   $allQueries = $query1->union($query2)->union($query3)->get();
 } elseif ($type!=null && $Des!=null){
   $query1 = Activity::where('id', $type);   
   $query2 = Activity::where('des_id', $Des);
   $allQueries = $query1->union($query2)->get();
 } elseif ($type!=null && $cou!=null){
   $query1 = Activity::where('id', $type);   
   $query2 = Activity::where('des_id', $cou);
   $allQueries = $query1->union($query2)->get();
 } elseif ($type!=null && $cou!=null){
   $query1 = Activity::where('id', $type);   
   $query2 = Activity::where('des_id', $Des);
   $allQueries = $query1->union($query2)->get();
 } elseif ($Des!=null && $cou!=null){
   $query1 = Activity::where('id', $cou);   
   $query2 = Activity::where('des_id', $Des);
   $allQueries = $query1->union($query2)->get();
 } elseif ($type!=null){
   $query1 = Activity::where('id', $type)->get();
 } elseif ($Des!=null){
   $query1 = Activity::where('id', $Des)->get();
 } elseif ($cou!=null){
   $query1 = Activity::where('id', $cou)->get();
 }
Sign up to request clarification or add additional context in comments.

6 Comments

I will use it but I want for everyone of the three of them .Its a user he picks destination if the destination is not null i want the Query to be bulit.If he picks the type only i want the Query to be built.
you can do it with if else like my answer
i will try it but i did something similar and did not work it kept asking me to use the first condition (Choose destination) no matter what.
is there a way to make this without ifs?
I don't know any better way without if else
|

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.