I need to add a where condition to my search function, but I don’t know how to add this to my final results. Now, for an example: now, my search is searching for the name in table career_solutions
, but it searching in the entire table, and I need to search only by category 2 for example. So, the final result should be … Search for example
in category 2
(which is Courses).
Here is my form:
<form action="/topic/{{strtolower($category->category)}}/career-solutions" method="POST" role="search"> {{ csrf_field() }} <div class="input-group"> <input type="text" class="form-control" name="q" placeholder="Search content"> <span class="input-group-btn"> <button type="submit" class="btn btn-default"> <span class="glyphicon glyphicon-search"></span> </button> </span> </div> </form>
{{ $category->category }} returns me the category name, not the ID of that.
Here is my controller:
public function searchCareer { $q = Input::get ( 'q' ); $user = CareerSolution::where ( 'subject', 'LIKE', '%' . $q . '%' ) ->join('role_users' , 'role_users.user_id', '=', 'career_solutions.user_id') ->join('roles' , 'roles.id', '=', 'role_users.role_id') ->join('users', 'users.id', '=', 'career_solutions.user_id') ->join('categories', 'categories.id', '=', 'career_solutions.topic_category_id') ->orWhere ( 'career_solutions.user_id', 'LIKE', '%' . $q . '%' ) ->orWhere ( 'career_solutions.id', '=', 'events.subject') ->orWhere('career_solutions.topic_category_id' ,'=', $category->id) ->orWhere ( 'career_solutions.user_id', '=', 'users.username') ->select('career_solutions.id as id','subject','users.id as user_id','username', 'profile_picture', 'role_id', 'optional', 'topic_category_id','categories.category') ->get (); $data['test'] = 'view-career-solutions'; $data['link'] = 'search-career-solutions'; $data['type'] = 'Career Solution'; $data['typee'] = 'briefcase fa-'; $topic_id = CareerSolution::select('id', 'subject') ->get(); if (count ( $user ) > 0) return view ( 'category-search',$data )->withDetails ( $user )->withQuery ( $q )->with(compact('topic_id'))->with(compact('account')); else return view ( 'category-search',$data )->withMessage ( 'No Details found. Try to search again !' ); }
Advertisement
Answer
You could add another hidden field to pass the category like :
<input type="hidden" name="c" value="{{$category->id }}">
Then in the controller we need to get the parameter :
$c = Input::get ( 'c' );
And as a last step add it before the select like :
... ->where('topic_category_id', $c) ->select('career_solutions.id as id','subject','users.id as user_id','username', 'profile_picture', 'role_id', 'optional', 'topic_category_id','categories.category') ->get();