如何使用Laravel中的控制器访问模型函数


How to access a model function using a controller in Laravel?

你能帮我吗?我正在使用Laravel创建自己的登录过程。顺便说一句,我在拉拉维尔还是个新手,我真的没有足够的知识。

我的场景是,我在控制器中创建了一个带有参数的代码,该代码可以访问模型函数。该模型函数将查找用户数据在数据库中是否正确或匹配。但我还没有创建它。我只是想看看我的参数中的数据在模型中是否可以访问。

问题是在模型中,我无法访问参数值。

这是我的代码:

在我的控制器中

public function login() {
    $rules = array(
        'employee_id'       =>  'required',
        'employee_password' =>  'required'
    );
    $validate_login = Validator::make(Input::all(), $rules);
    if($validate_login->fails()) {
        $messages = $validate_login->messages();
        return Redirect::to('/')->withErrors($messages);
    } else {
        $userdata = array(
            'id'        =>  Input::get('employee_id'),
            'password'  =>  Hash::make(Input::get('employee_password'))
        );
        $validateDetail = Employee::ValidateLogin($userdata); //pass parameter to model
    }
}

这是的模型函数

public function scopeValidateLogin($data) 
{
    fd($data); //fd() is my own custom helper for displaying array with exit()
}

在scopeValidateLogin()函数中,我计划使用查询生成器来验证登录。

这是我的路线

Route::model('employee','Employee');
Route::get('/','EmployeesController@index');
Route::get('/register', 'EmployeesController@register');
Route::post('/login','EmployeesController@login');
Route::post('/handleRegister', function() 
            {
                $rules = array(
                    'emp_code'      =>  'numeric',
                    'lastname'      =>  'required|min:2|max:15',
                    'firstname'     =>  'required|min:2|max:20',
                    'middlename'    =>  'min:1|max:20',
                    'password'      =>  'required|min:8|max:30',
                    'cpassword'     =>  'required|same:password'
                );
                $validate_register = Validator::make(Input::all(), $rules);
                if($validate_register->fails()) {
                    $messages = $validate_register->messages();
                    return Redirect::to('register')
                                        ->withErrors($messages)
                                        ->withInput(Input::except('password','cpassword'));
                } else {
                    $employee = new Employee;
                    $employee->emp_code     = Input::get('emp_code');
                    $employee->lastname     = Input::get('lastname');
                    $employee->firstname    = Input::get('firstname');
                    $employee->middlename   = Input::get('middlename');
                    $employee->gender       = Input::get('gender');
                    $employee->birthday     = Input::get('birthday');
                    $employee->password     = Hash::make(Input::get('password'));
                    $employee->save();
                    Session::flash('success_notification','Success: The account has been successfully created!');
                    return Redirect::action('EmployeesController@index');
                }
            }
        );

现在,在运行fd($data)之后,我的浏览器加载了一系列数组,然后就会崩溃。我不知道发生了什么,但我认为它向我的模型发送了多个请求。

我访问控制器内部模型的方式是否正确?或者有什么最好的方法吗?

您错误地使用了MVC框架。您应该在控制器内部评估输入和登录,而不是使用模型内部的方法。