Multiple Column With Same Search String in Laravel

Multiple column with same search string in laravel

Try this.

$field = ['name','id','address'];
$name = DB::Table('bookinfo')->Where(function ($query) use($string, $field) {
for ($i = 0; $i < count($field); $i++){
$query->orwhere($field, 'like', '%' . $string .'%');
}
})->get();

Laravel QueryBuilder multiple columns match in the same WhereIn

The query syntax you want to get:

SELECT id FROM my_db.countries WHERE (name, enabled) IN (("France", 1), ("Spain", 0));

is exclusive to Oracle. The whereIn method of Laravel supports (String, Array), so I think you only have the options to do it through raw queries or using the solution proposed by V-K

How to search from multiple columns in laravel with comma separated values

If the main_category is VARCHAR or similar, you need to do this:

Company::where(function($q) use($categoryId) {
$q->where('main_category', 'like', '%,' . $categoryId . ',%')
$q->orWhere('main_category', 'like', $categoryId . ',%')
$q->orWhere('main_category', 'like', '%,' . $categoryId)
})
->where($matchThese)->get();

But a much better way to handle this is to use many to many relationship between Company and Category models. You need to create a new pivot table category_company and use it as described in the docs.

Search multiple words in same column on laravel

You could instantiate an array containing your search strings, then use the ->orWhere() method on the collection to find them.

$keywords = ['hello', 'tea', 'break'];
$result = $query->where(function ($query) use ($keywords) {
foreach ($keywords as $keyword) {
$query->orWhere('title', 'LIKE', "%$keyword%");
}
});

Laravel multiple column eloquent search query

Simply chain where for each field you need to search through:

// AND
$results = SomeModel::where('location', $location)->where('blood_group', $bloodGroup)->get();

// OR
$results = SomeModel::where('location', $location)->orWhere('blood_group', $bloodGroup)->get();

You can make it easier to work with thanks to the scopes:

// SomeModel class
public function scopeSearchLocation($query, $location)
{
if ($location) $query->where('location', $location);
}

public function scopeSearchBloodGroup($query, $bloodGroup)
{
if ($bloodGroup) $query->where('blood_group', $bloodGroup);
}

// then
SomeModel::searchBloodGroup($bloodGroup)->searchLocation($location)->get();

Just a sensible example, adjust it to your needs.

Laravel Eloquent search multiple fields and relationship

Use the orWhereHas() method:

Machine::latest()
->where('serial', 'like', '%' . $search . '%')
->orWhere('type', 'like', '%' . $search . '%')
->orWhereHas('warranty', function($q) use($search) {
$q->where('first_name', 'like', '%' . $search . '%');
})
->get();


Related Topics



Leave a reply



Submit