【发布时间】:2014-12-24 17:12:52
【问题描述】:
你能帮我解决这个问题吗?我正在使用 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() 函数中,我计划使用 Query Builder 来验证登录。
这是我的路线
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) 后,我的浏览器会加载一系列数组,然后它会崩溃。 我不知道发生了什么,但我认为它正在向我的模型发送多个请求。
我是否以正确的方式访问控制器内的模型?或者有什么最好的方法吗?
【问题讨论】: