Skip to content

Instantly share code, notes, and snippets.

@nkeena
Last active June 11, 2021 16:45
Show Gist options
  • Save nkeena/4e9ca99f07856eae8d4e11c133f4d073 to your computer and use it in GitHub Desktop.
Save nkeena/4e9ca99f07856eae8d4e11c133f4d073 to your computer and use it in GitHub Desktop.
Search concatenated columns in MySQL while Ignoring NULL values
<?php
public function scopeSearch($query, $search)
{
$search = addslashes($search); // in case the search has quotes
$query->when(
$search,
fn ($q) => $q->whereRaw(
"CONCAT_WS(' ', title, first_name, last_name, suffix) LIKE '{$search}%'"
)
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment