Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

queeneldza's avatar

Check HASH-256 Laravel

Hi, I have a system that requires a homeowner to submit a form about their guest's details. When they submit a form, each will be assigned a unique 6-digit code. This code will be hashed with SHA-256. In the admin's view, there is a list of all submitted forms. I want to do a search function where when I enter the code, the system will look through the hashed code and check if it exists or not.

This is my GuestController:

public function index()
    {
        //returns admin's view
        $guest = Guest::all();
        return view('pages.guest.index', compact('guest'));
    }
public function store(Request $request)
    {
        $guest = new Guest;
        $guest->code = random_int(100000, 999999);
        $guest->hash = hash('sha256', $guest['code']);  
        $guest->owner_id = Auth::user()->id;
        $guest->owner = Auth::user()->name;
        $guest->unit = Auth::user()->unit;
        $guest->guestname = $request->input('guestname');
        $guest->guestphone = $request->input('guestphone');
        $guest->guestic = $request->input('guestic');
        $guest->guestcar = $request->input('guestcar');
        $guest->numberofguests = $request->input('numberofguests');
        $guest->datevisit = $request->input('datevisit');
        $guest->timevisit = $request->input('timevisit');
        $guest->save();
        
        return redirect('show-pass')->with('status', 'Guest Added Successfully');
    }
public function search(Request $request)
    {
        //Get the search value from the request
        $search = $request->input('search');

        //Search in the code from the list
        $guest = Guest::query()
                ->where('code', 'LIKE', "%{$search}%")
                ->get();
               
        //Return the search view with the results compacted
        return view('pages.guest.search', compact('guest'));

    }

This is my search result blade:

<div class="card-body">
                @if($guest->isNotEmpty())
                        @foreach ($guest as $item)
                            <div class="post-list">          
                                <p>Owner : {{ $item->owner }}</p>
                                <p>Unit : {{ $item->unit }}</p>                 
                                <p>Guest Name : {{ $item->guestname }}</p>
                                <p>Guest Phone Number : {{ $item->guestphone }}</p>
                                <p>Guest IC Number : {{ $item->guestic }}</p>
                                <p>Guest Car Number : {{ $item->guestcar }}</p>
                                <p>Date : {{ $item->datevisit }}</p>
                                <p>Time : {{ $item->timevisit }}</p>
                            </div>
                        @endforeach
                        @else 
                        <div>
                        <h4>No guests with the code was found</h4>
                        </div>
                @endif
             </div>

How can I edit my search method to be able to do so? May I get some help?

0 likes
12 replies
Snapey's avatar

what's the point of hashing the code?

You cannot search within hashes without loading every record and checking them one by one

queeneldza's avatar

@Snapey my project is entitled hashed qr code authentication gating system, where it generates qr code that contains the unique code. However, in the back end, I want to hash the code so that nobody can create their own unique code. So, I want to check if the hashed code exists or not. I want to do it kinda like password check. Can you teach me how?

Snapey's avatar

@queeneldza password hashing only works by first finding the user ( by email) and then checks their hashed password

Sinnbeck's avatar

@queeneldza I was talking about this (as it isnt unique)

$guest->code = random_int(100000, 999999);
queeneldza's avatar

@Snapey oh, I get it. Is it possible if I want to do Hash::check in my case? Does it work if I use SHA-256 instead of Laravel's bcrypt?

Snapey's avatar
Snapey
Best Answer
Level 122

@queeneldza You need to rethink. All you have is a 6 digit number that will have collisions in only a small amount of time. What this means is that you will scan the QR code and have two results and not know which it should be.

Yes you can search for a hash value (as its just a string) but you would be better implementing a simple random string, UUID or using HashIDs based on the record primary key.

Personally I would use HashIDs since you can guarantee no collisions and the hashid will be as small as it can be.

https://github.com/vinkla/laravel-hashids

Sinnbeck's avatar

You are already saving the code? Why not search by it (as you seem to be already doing)?

BTW. How can you be sure that code is unique?

Snapey's avatar

@Sinnbeck good point the code will probably have collisions within a few thousand records

1 like

Please or to participate in this conversation.