I'm using laravel 5.1. Now I want to retrieve check if a certain password is already defined in the database. Here's my database schema
/* Accounts table */
Schema::create('accounts', function (Blueprint $table) {
$table->increments('id');
$table->integer('pharmacist_id')->unsigned()->index();
$table->foreign('pharmacist_id')->references('id')->on('pharmacists')->onDelete('cascade');
$table->string('username')->unique();
$table->string('password', 70);
$table->string('rights');
$table->rememberToken()
$table->timestamps();
});
/* Pharmacists Table */
Schema::create('pharmacists', function (Blueprint $table) {
$table->increments('id');
$table->integer('pharmacy_id')->unsigned()->index();
$table->foreign('pharmacy_id')->references('id')->on('pharmacies')->onDelete('cascade');
$table->string('fname');
$table->string('mname');
$table->string('lname')
$table->date('bdate');
$table->string('email');
$table->string('contact');
$table->timestamps();
});
Now what I want is to check if a certain password is already defined in a certain pharmacy_id it looks something like this
$accounts = Account::whereHas('pharmacist', function($query) {
return $query->where('pharmacy_id', Auth::user()->id);
})->where('password', $password)->get();
But it seems that the password is only being passed as a plain text and not encrypted. Also I tried using this method
where('password', bcrypt($password))
where('password', Hash::make($password))
where('password', Crypt::encrypt($password))
But none of this works. Any solution guys? I'm thinking of something like this and I'm not sure if this is possible
$is_valid = Auth::validate(array('pharmacist.pharmacy_id' => Auth::user()->id, 'password' => $value));
Because if I used the below code I can able to check if the user has inputted the valid password.
$is_valid = Auth::validate(array('id' => Auth::user()->id, 'password' => $value));
It's easy to check if the username and password match using the Auth::validate
but the needed checking is to check if a certain pharmacist already inputted this specific password. So basically its kinda like looping in all the accounts and check if their password is the same as this specific password.
Here's what I have so far but this has some problem. If a certain pharmacy has 1000+ user then this will loop 1000x which is not optimized and not a good solution
$accounts = Account::whereHas('pharmacist', function($query) {
return $query->where('pharmacy_id', Auth::user()->id);
})->get();
foreach($accounts as $account) {
if (Hash::check($value, $account->password)) {
// FOUND!!!
}
}
To make it short
Pharmacy has many Pharmacists Pharmacist has one account
Now I want to check if a certain Pharmacy has an account password of "certain password" so its like I need to check all account belonging to a certain pharmacists and that pharmacy belongs to a certain pharmacy
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire