Files
freeu-project/app/Http/Requests/StoreAdminRequest.php
Ritikesh yadav c661166e1d first commit
2024-03-28 14:52:40 +05:30

63 lines
1.5 KiB
PHP

<?php
namespace App\Http\Requests;
use App\Models\User;
use Illuminate\Foundation\Http\FormRequest;
class StoreAdminRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'name' => 'required',
// 'email' => 'required|unique:users,email',
'email' => 'required',
'contact_number' => 'required|unique:users,contact_number',
];
}
public function messages()
{
return [
'required' => 'The :attribute field is required.',
'unique' => 'The :attribute field must be unique.',
];
}
public function validated()
{
$password = $this->generateRandomString();
return [array_merge(parent::validated(), [
'role' => User::ADMIN,
'password' => bcrypt($password)
]), $password];
}
function generateRandomString($length = 10)
{
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$charactersLength = strlen($characters);
$randomString = '';
for ($i = 0; $i < $length; $i++) {
$randomString .= $characters[random_int(0, $charactersLength - 1)];
}
return $randomString;
}
}