Simplest method
The simplest method is nesting the where
and orWhere
methods of the QueryBuilder:
<?php
$users = DB::table('users')
->where('name', 'like', 'Albert%')
->orWhere('name', 'like', 'Alberto%')
->orWhere('name', 'like', 'Ana%')
->get();
?>
More general applicable for large array of names
You can use the whereRaw
method of the QueryBuilder, and combine this with some basic php and sql. In this way MySQL injection is also automatically prevented.
<?php
$names = [
'Abert',
'Alberto',
'Ana'
];
$conditions = [];
$sqlnames = [];
// of course you can append the wildcards directly,
// but this is more general applicable
foreach($names as $name){
$sqlnames[] = $name . '%';
$conditions[] = '`name` LIKE ?';
}
// run the query
$users = DB::table('users')
->whereRaw(implode(' OR ', $conditions), $sqlnames)
->get();
?>