63 lines
1.5 KiB
PHP
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;
|
||
|
|
}
|
||
|
|
}
|