i have a problem , an error when return array or variable from function.. here is my code:
in Traits\\UserTrait.php
public function login()
{
$errors = new MessageBag; // initiate MessageBag
$username = Input::get('email');
$password = Input::get('password');
return compact('username', 'password','email');
}
and in UserController.php
public function __construct(user $model)
{
$this->model=$model;
}
public function login()
{
$this->model->login();
if(filter_var($username, FILTER_VALIDATE_EMAIL)) {
$user = User::where('email',$request['email'])->first();
if($user && $user->email_active != 1)
{
$errors = new MessageBag(['email' => [__('auth.emailVerify')]]);
return Redirect::back()->withErrors($errors)->withInput(Input::except('password'));
}
}
}
actually in user model, i put use UserTrait;
You might be calling a method that returns something, but you are not actually doing anything with the return at all. (Returning from a method doesn't magically import variables into the callers scope)
$this->model->login(); // never assigning the result to anything
Ofcourse you are getting undefined $username
because in UserController@login
there is no variable named $username
.
This would at least get that username
key and assign it to a variable:
$data = $this->model->login();
$username = $data['username'];
This is not how traits work, they do no magically fill variables that are missing. On the line
if(filter_var($username, FILTER_VALIDATE_EMAIL)) {
Your are using $username
, which is not set.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.