Data not searched for space words
My search is not working properly,if i space the words and click submit button it gives null results but it works fine for not space words.why?
public function searchR()
{
$term = Input::get('auto');
$queries = DB::table('jobs')
->where('profession', 'LIKE', '%'.$term.'%')
->orWhere('country', 'LIKE', '%'.$term.'%')
->get();
dd($queries);
return view('autocomplete',compact('queries'));
}
form
{!! Form::open(['url' => 'autocomplete/data', 'method' => 'GET']) !!}
<?= Form::text('auto', '', array('id' => 'auto'))?>
{!!Form::submit('submit')!!}
<?= Form::close() ?>
Well it's because you search for the full string. So when you search for laracasts is awesome you would get this in your query
SELECT * FROM jobs WHERE profession like '%laracasts is awesome%'
So if you have no results for that, it won't work.
What you need to do is split the words and add more where statements
$words = explode(' ', $term);
$jobs = DB::table('jobs')->where(function ($query) {
foreach($words as $word) {
$query->orWhere('profession', 'LIKE', '%' . $word . '%');
}
})->orWhere(function ($query) {
foreach($words as $word) {
$query->orWhere('country', 'LIKE', '%' . $word . '%');
}
})->get();
// If you want to see the query you can replace get() with toSql()
})->toSql();
dd($jobs);
@bobbybouwmann Thanks i marked your answer as solved but you forget to use "use" keyword
$jobs = DB::table('jobs')->where(function ($query)use($words)
Aah sorry, I did it out of my head ;)
Please or to participate in this conversation.