I have a string passed from a form which is full name.
in my database I store first name and last name.. I've split the string using the following:
$name = explode(" ", $request->name);
$lastname = array_pop($name);
$firstname = implode(" ", $name);
this works great, however, if the user doesn't enter a surname in the field then the above doesn't work as the lastname becomes the first.
Am I missing something?
$query->where(function ($q) use ($columns, $value) { foreach ($columns as $column) { $q->orWhere($column, 'like', "%{$value}%"); } });
I've split the string using the following: $name = explode(" ", $request->name); $lastname = array_pop($name); $firstname = implode(" ", $name); this works great, however, if the user doesn't enter a surname in the field then the above doesn't work as the lastname becomes the first.
This is what I've used for splitting names:
$splitName = explode(' ', $name, 2); // Restricts it to only 2 values, for names like Billy Bob Jones
$first_name = $splitName[0];
$last_name = !empty($splitName[1]) ? $splitName[1] : ''; // If last name doesn't exist, make it empty
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With