Last active
June 11, 2021 16:45
-
-
Save nkeena/4e9ca99f07856eae8d4e11c133f4d073 to your computer and use it in GitHub Desktop.
Search concatenated columns in MySQL while Ignoring NULL values
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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