0

I built small search form helps me search in jobsTable from db

jobs Table has jobTitle, jobCompany, jobGovernorate, jobLocation and created_at column

And posted data from search form:

{"jobTitle":"designer","jobCompany":null,"jobGovernorate":null,"jobLocation":null,"postingDate":"ad"}

I mean some input may be null, so how to write dynamic where clause for inputs have value, i want search engine able to search by JobTitle, JobCompany or All inputs if have value.

Hint: fields name and table columns name are same

if there's a way to retrieve jobs from my db like

DB::table('jobs')->where($request->all())

Sorry for poor english, I don't know how to explain my question

AhmedEls
  • 708
  • 2
  • 9
  • 25

2 Answers2

2

I prefer to define all search fields.

$searchFields = ['jobTitle','jobCompany','jobGovernorate','jobLocation','postingDate'];
$jobQuery = DB::table('jobs');
foreach ($searchFields as $field) {
    if ($request->has($field)) {
        $jobQuery->where($field, $request->input($field));
    }
}
$results = $jobQuery->get();
sisve
  • 19,501
  • 3
  • 53
  • 95
xuma
  • 466
  • 3
  • 5
0

you have to code the query, I mean, what are you looking for? something like

DB::table('jobs')->where([
['jobTitle',  'like', $request->jobTitle . '%'],
['jobCompany', 'like', $request->jobCompany . '%']])->get();
Alex Angelico
  • 3,710
  • 8
  • 31
  • 49