One solution to prevent overwriting the hash in case of an error is to store the hash in the session before rendering the create view. Then, in the create function, check if the hash exists in the session and use that instead of generating a new one. Here's an example:
// In the controller function that renders the create view
$myHash = session('myHash') ?? \bin2hex(\random_bytes(8));
session(['myHash' => $myHash]);
return view('create', ['myHash' => $myHash]);
// In the create function
$myHash = session('myHash');
$hashValues = HashValue::where('hash_value', $myHash)->first();
if (!$hashValues) {
// Handle error
}
// Use $myHash and $hashValues as needed
This way, the hash is only generated once per session and is not overwritten when the form is submitted with errors.