skip to Main Content

Basically I want to be able to query my Laravel MySQL database and get a member details based on the ID provided on the search form. I am currently using the Laravel ‘LIKE’ feature for this query but it isn’t just enough.

For example, i have a member with the ID of AVC/"A"DIVWARRI/0059 . I only want to display this member details when the input search matches all of the string characters or letters and also want it to be case sensitive too. ( for example , do not display user data even though the id is the same but in lowercase avc/"a"divwarri/0059 .

With the Laravel ‘LIKE’ feature even though I only input the word ‘DIV’ which is a part of the whole member ID AVC/"A"DIVWARRI/0059 , it still gets to show the member details , of which I do not want

This is the code I am using below for the controller . How can I improve on this to meet my needs??

public function id_check(Request $request)
{
    $id = $request->id;

    $member = Member::when($id, function ($query, $id) {
        return $query->where('id_no', 'like', "%$id%")->get();
    });


    if ($member->isEmpty()) {
        return view('home.notfound');
        
    } else {
        return view('home.id_verification', [
            'member' => $member
        ]);
    }
}

2

Answers


  1. If you want to force a case sensitive search using like because your collation is already case insensitive, simply cast it as binary:

    $query->where('id_no', 'like binary', "%$id%")->get();
    

    If you want to only match the last name (example DIVWARRI) based on your format of AVC/"A"DIVWARRI/0059 then something like this should work:

    $query->where('id_no', 'like binary', "%/"%"$id/%")->get();
    
    Login or Signup to reply.
  2. If i understand correctly, think you need the equal operator to match the exact string like this:

    $query->where('id_no', '=', $id)->get();
    

    Update

    to match the case sensitive strings you can try to use the Binary conversion of the field like this:

    $query->where(DB::raw('BINARY `id_no`') '=', $id)->get();
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search