0

当我想打开管理员身份验证系统页面时,它会收到以下错误。另外,我正在使用 localhost XAMPP 服务器。收到的第一个错误:已用完允许的 536870912 字节的内存大小。现在我找到了解决方案,所以我增加了 php.ini 文件的内存大小限制。

然后第二个收到错误:超过了 60 秒的最大执行时间。现在我找到了解决方案,因此我增加了 php.ini 文件的最大执行时间。

然后页面无法打开,因为该页面无法处理请求。有没有人有解决方案?

路线:

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');
    });
});

验证码

    '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,

应用程序/用户.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'];
}

登录控制器

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'));
    }
}

注册控制器

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']),
        ]);

    }


}

4

1 回答 1

1

在您的路线文件中,您可以更改一些内容。

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'));
});

改变

$student = User::all()->where('user_role', 'student')->count();
$employee = User::all()->where('user_role', 'employee')->count();

$student = User::where('user_role', 'student')->count();
$employee = User::where('user_role', 'employee')->count();

为什么? User::all

  • 使select * from users
  • Illuminate\Database\Eloquent\Collection使用数据库刚刚返回的所有行创建一个对象。

当您与大量用户一起工作时,这可能会占用大量内存。您进行了两次此查询。然后,使用->where(...)过滤Collection,这在与很多用户一起工作时可能会占用大量内存。然后,您使用->count()将其减少为单个值,这意味着您查询并生成的所有数据都是徒劳的Collection

如果你只是想要一个计数,让数据库来做。User::where('user_role', 'student')->count();转换为简单的select count(*) from users where user_role = student.

于 2020-08-03T17:37:53.090 回答