uccdev
7 months ago
358
3
Laravel

Optimal way to pass user name? (Based off data from web endpoint)

Posted 7 months ago by uccdev

I'm implementing a search option which lets someone find a user from a webpage, depending on their name.

Look at this code here:

 public function searchByUser(Request $request) {
    $first = $request->firstname;
    $sur = $request->surname;
    $userURL = "https://my.website.com/?search_term=$first+$sur";

If someone were to type "Claire" for the firstname with no surname, it'd give me every user with "Claire" in the name, and the same rules for the surname, as well as combined together.

Now, this is functional, but I'm wondering three things:

  1. Is it optimal?
  2. At the moment I've had to split the name into two variables: what if I wanted to keep them in one? At the moment that doesn't work because my code can't interpret the split too well (e.g instead of "John%20Smith", it sees "John Smith", which gives me a broken result)
  3. Furthermore, what if someone wants a middle name, or has a more complex name to put in? e.g a middle name, like "John Jacob Smith" I suppose I can make a "middle_name" variable, but if there's another complication to the name, what then? Is there a one-size-fits-all for this sort of thing?

If anyone has any advice, that'd be helpful

EDIT: I should clarify, this is based on data from a webpage endpoint, so MySQL solutions will unfortunately not work.

Please sign in or create an account to participate in this conversation.