Reputation: 85
When I want to open the admin Authentication system page so it will receive the following errors. also, I'm using localhost XAMPP server. first Error received: The allowed memory size of 536870912 bytes exhausted. now I have found the solution so I have increased the memory size limit from php.ini file.
then second Error received: The maximum execution time of 60 seconds exceeded. now I have found the solution so I have increased the maximum execution time from php.ini file.
then the page doesn't open because this page is unable to handle the request. Is there anyone have a solution?
Routes:
Auth::routes();
Route::middleware('auth')->group(function (){
Route::get('/dashboard',function (){
return view('dashboard');
});
Route::get('/faculty', function (){
$title='Faculty Console';
return view('faculty-dashboard',compact('title'));
});
Route::get('/student', function (){
$title='Student Console';
return view('user-dashboard',compact('title'));
});
Route::prefix('admin')->group(function () {
Route::get('/', function (){
$student=User::all()->where('user_role','student')->count();
$employee=User::all()->where('user_role','employee')->count();
$title='Admin Console';
return view('admin.admin-dashboard',compact('title','student','employee'));
});
Route::resource('selection','SelectionController');
Route::get('selection/{id}/pivot-create/','SelectionController@pivotCreate')->name('pivotCreate');
Route::get('selection/{id}/pivot-destroy/','SelectionController@pivotDestroy')->name('pivotDestroy');
});
});
Auth.php
'defaults' => [
//'guard' => 'web',
'guard' => 'user',
'passwords' => 'users',
],
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'user' => [
'driver' => 'session',
'provider' => 'users',
],
'api' => [
'driver' => 'token',
'provider' => 'users',
'hash' => false,
],
],
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\User::class,
],
],
'passwords' => [
'users' => [
'provider' => 'users',
'table' => 'password_resets',
'expire' => 60,
'throttle' => 60,
],
],
'password_timeout' => 10800,
App/User.php
class User extends Authenticatable
{
use Notifiable;
protected $guarded = [];
protected $table='users';
protected $hidden = ['password', 'remember_token',];
protected $casts = [
'email_verified_at' => 'datetime',
];
protected $dates = ['created_at', 'updated_at'];
}
LoginController
class LoginController extends Controller
{
use AuthenticatesUsers;
public function username()
{
return 'user_name';
}
protected function redirectTo(){
if (Auth::user()->user_role == 'admin' && Auth::user()->status == '1'){
//alert()->message('Welcome','Admin')->closeOnClickOutside();
return '/admin';
}
elseif (Auth::user()->user_role == 'faculty' && Auth::user()->status == '1'){
//alert()->message('Welcome','Faculty')->closeOnClickOutside();
return '/faculty';
}
elseif (Auth::user()->user_role == 'student' && Auth::user()->status == '1'){
//alert()->message('Welcome','Student')->closeOnClickOutside();
return '/student';
}
else
return redirect('/login');
}
public function logout()
{
Auth::logout();
//session()->flash('msg_logout', 'Panel System Successfully Logout');
alert()->success('You have been logout successfully','GoodBye');
//session()->flush();
return redirect('/login');
}
public function showLoginForm(){
$title='TheMSC-Sign-In Panel-System';
return view('log-in',compact('title'));
}
}
RegisterController
class RegisterController extends Controller
{
use RegistersUsers ;
protected $redirectTo = RouteServiceProvider::HOME;
protected function validator(array $data)
{
return Validator::make($data, [
'cnic' => ['required','string'],
'user_name'=>[ 'required','string','unique:users','regex:[a-z\d-]'],
'user_role'=>[ 'required','string'],
'email' => [ 'required', 'string', 'email:filter,dns'],
'password' => [ 'required', 'string', 'confirmed','regex:[a-zA-Z\d $&+,:;=?@#|\'<>.^*()%!-]'],
]);
}
protected function create(array $data)
{
return User::create([
'cnic' => $data['cnic'],
'user_name' => $data['user_name'],
'user_role' => $data['user_role'],
'email' => $data['email'],
'password' => Hash::make($data['password']),
]);
}
}
Upvotes: 0
Views: 201
Reputation: 15786
In your routes file there's something you could change.
Route::get('/', function () {
$student = User::all()->where('user_role', 'student')->count();
$employee = User::all()->where('user_role','employee')->count();
$title = 'Admin Console';
return view('admin.admin-dashboard', compact('title','student','employee'));
});
Change
$student = User::all()->where('user_role', 'student')->count();
$employee = User::all()->where('user_role', 'employee')->count();
to
$student = User::where('user_role', 'student')->count();
$employee = User::where('user_role', 'employee')->count();
Why?
User::all
:
select * from users
Illuminate\Database\Eloquent\Collection
object with all the rows the database just returned.This can be memory intensive when you're working with a lot of users. You make this query twice. And then, use ->where(...)
to filter the Collection
, which again, may be memory intensive when working with a lot of users. Then, you use ->count()
to reduce it to a single value, meaning all the data you queried and made into a Collection
was for nothing.
If you just want a count, let the database do it. User::where('user_role', 'student')->count();
translates to a simple select count(*) from users where user_role = student
.
Upvotes: 1