laravel 5中多个auth Attemp()的问题

时间:2016-01-07 08:56:56

标签: php laravel-5

我对代码感到有点困惑。

auth::attempt()对我不起作用,任何人都可以帮我吗?

Controller.php这样

public function postLogin(Request $request) 
{
    ....
    if (\Auth::attempt('frontendUsers', $userdata)) {   
        return Redirect::back()->with('success',true);
    } else {
        return redirect('login');
    }
}

view.blade.php

<form name="myForm1" id="loginform" role="form" method="post" action="{{ action('Auth\loginController@postLogin') }}">
    <input type="text" placeholder="Card No" name="cardno"/>
    <input type="password" placeholder="Password" name="password"/>
    <div class="col-lg-6 col-xs-3 clear-both height">
        <label class="remember">Remember Me:</label>
        <input type="checkbox" class="remember-check"/>
    </div>
    <div class="login-submit">
        <input type="submit" value="Sign in">
    </div>
</form>

model.php

namespace App;

use Illuminate\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Foundation\Auth\Access\Authorizable;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;


class loginModel extends Model implements AuthenticatableContract,
                                    AuthorizableContract,
                                    CanResetPasswordContract
{
    use Authenticatable, Authorizable, CanResetPassword;
    /**
     * The database table used by the model.
     *
     * @var string
     */
    protected $table    = 'frontend_users';

    protected $hidden = ['password', 'remember_token'];
}

route.php

Route::get('logout','Auth\loginController@getLogout');
Route::controller('/','Auth\loginController');
Route::controller('/login', 'Auth\loginController');

我的问题是当我运行我的代码时出现此错误:

InvalidArgumentException in AuthenticatesUsers.php line 42: Undefined property frontend_users not found in auth.php

我的config / auth.php

return [
        'multi' => [
            'users' => [
                'driver' => 'eloquent',
                'model'  => App\User::class,
                'table'  => 'users'
            ],
            'frontendUsers' => [
                'driver' => 'eloquent',
                'model'  => App\loginModel::class,
                'table'  => 'frontend_users'
            ]
         ],
         'password' => [
                'email' => 'emails.password',
                'table' => 'password_resets',
                'expire' => 60,
            ],
        ];

相同的代码适用于另一个项目

2 个答案:

答案 0 :(得分:0)

我见过Laravel 5没有像你写的任何方法。 \ Auth :: attempt(&#39; frontendUsers&#39;,$ userdata)。请再次阅读文档laravel网站中的multi-auth。

验证::后卫(&#39; frontendUsers&#39;) - &GT;尝试($用户数据)

答案 1 :(得分:0)

来自Laravel文档:https://laravel.com/docs/5.1/authentication#authenticating-users

  

attempt方法接受一组键/值对作为其第一个&gt;参数。数组中的值将用于在&gt;数据库表中查找用户。

你只需要传递作为用户名的属性和另一个作为参数的密码作为尝试方法