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
Query With Multiple Values in a Column
Check If Username and Email Already Exists in Database
How to Run a PHP Script in the Background After a Form Is Submitted
How to Show Blob Image in HTML
Add Space After Every 4Th Character
Unable to Upload File Greater Than 2Mb in Laravel
Easiest Way to Replace All Characters in Even Positions in a String.
Saving Multiple Records in a Laravel Eloquent Create
How to Increase Maximum Execution Time in Laravel
Fatal Error: Call to Undefined Function Sqlsrv_Connect()
Convert Svg Image to Png With PHP
A Way to Determine Browser Width in PHP Without JavaScript
Find Combination(S) Sum of Element(S) in Array Whose Sum Equal to a Given Number
Laravel Print Array in Blade PHP
How to Store .Pdf Files into MySQL as Blobs Using PHP
Codeigniter: Fatal Error Call to Undefined Function Mysqli_Init()