محسن
3 سال پیش توسط محسن مطرح شد
1 پاسخ

شناخته نشدن attempt در لاراول

سلام.من یه گارد جدید تعریف کردم.وقتی تو کنترلر ازش استفاده میکنم تابع attempt رو نمیشناسه .میشه راهنمایی کنید؟(لاراول 8)

<?php

return [

    /*
    |--------------------------------------------------------------------------
    | Authentication Defaults
    |--------------------------------------------------------------------------
    |
    | This option controls the default authentication "guard" and password
    | reset options for your application. You may change these defaults
    | as required, but they're a perfect start for most applications.
    |
    */

    'defaults' => [
        'guard' => 'web',
        'passwords' => 'users',
    ],

    /*
    |--------------------------------------------------------------------------
    | Authentication Guards
    |--------------------------------------------------------------------------
    |
    | Next, you may define every authentication guard for your application.
    | Of course, a great default configuration has been defined for you
    | here which uses session storage and the Eloquent user provider.
    |
    | All authentication drivers have a user provider. This defines how the
    | users are actually retrieved out of your database or other storage
    | mechanisms used by this application to persist your user's data.
    |
    | Supported: "session", "token"
    |
    */

    'guards' => [
        'web' => [
            'driver' => 'session',
            'provider' => 'users',
        ],
        'api' => [
            'driver' => 'passport',
            'provider' => 'users',
            'hash' => false,
        ],
        'admin' => [
            'driver' => 'passport',
            'provider' => 'admins',
        ],
    ],

    /*
    |--------------------------------------------------------------------------
    | User Providers
    |--------------------------------------------------------------------------
    |
    | All authentication drivers have a user provider. This defines how the
    | users are actually retrieved out of your database or other storage
    | mechanisms used by this application to persist your user's data.
    |
    | If you have multiple user tables or models you may configure multiple
    | sources which represent each model / table. These sources may then
    | be assigned to any extra authentication guards you have defined.
    |
    | Supported: "database", "eloquent"
    |
    */

    'providers' => [
        'users' => [
            'driver' => 'eloquent',
            'model' => App\Models\User::class,
        ],
        'admins' => [
            'driver' => 'eloquent',
            'model' => App\Models\Admin::class,
        ],

        // 'users' => [
        //     'driver' => 'database',
        //     'table' => 'users',
        // ],
    ],

    /*
    |--------------------------------------------------------------------------
    | Resetting Passwords
    |--------------------------------------------------------------------------
    |
    | You may specify multiple password reset configurations if you have more
    | than one user table or model in the application and you want to have
    | separate password reset settings based on the specific user types.
    |
    | The expire time is the number of minutes that the reset token should be
    | considered valid. This security feature keeps tokens short-lived so
    | they have less time to be guessed. You may change this as needed.
    |
    */

    'passwords' => [
        'users' => [
            'provider' => 'users',
            'table' => 'password_resets',
            'expire' => 60,
            'throttle' => 60,
        ],
        'admins' => [
            'provider' => 'admins',
            'table' => 'password_resets',
            'expire' => 60,
            'throttle' => 60,
        ],
    ],

    /*
    |--------------------------------------------------------------------------
    | Password Confirmation Timeout
    |--------------------------------------------------------------------------
    |
    | Here you may define the amount of seconds before a password confirmation
    | times out and the user is prompted to re-enter their password via the
    | confirmation screen. By default, the timeout lasts for three hours.
    |
    */

    'password_timeout' => 10800,

];

کد مربوط به کنترلر:

class AdminController extends Controller
{
    public function login(AdminLoginRequest $request)
    {
        $check=Auth::guard('admin')->attempt($request->validated());
    }
}

ثبت پرسش جدید
محمد عابدی
تخصص : Senior Web Developer
@abedim910 3 سال پیش مطرح شد
0

@mohsenmoradi1851 درود

مدل

مدل ادمین رو به این شکل باید در بیارید

namespace App\Models;
use Illuminate\Foundation\Auth\User as Model;
class Admin extends Model
{
    protected $guarded = ['id'];
    protected $hidden = [
     'password', 'remember_token',
    ];
    public function getAuthPassword()
    {
     return $this->password;
    }
}

کنترلر ها

کنترلر هارو به این صورت بسازید

php artisan make:controller AdminController
php artisan make:controller Auth/Login/AdminController

کد AdminController میشه

namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
class AdminController extends Controller
{
    public function __construct()
    {
        $this->middleware('auth:admin');
    }
    public function index()
    {
        return view('home.admin');
    }
}

خوب $this->middleware('auth:admin'); باعث میشه فقط ادمین ها بتونن این کنترلر رو ببینن.
بعد از این نوبت میرسه به Auth Admin Controller

namespace App\Http\Controllers\Auth\Login;
use Illuminate\Support\Facades\Auth;
use App\Http\Controllers\Auth\LoginController as DefaultLoginController;
class AdminController extends DefaultLoginController
{
    protected $redirectTo = '/admin/home';
    public function __construct()
    {
        $this->middleware('guest:admin')->except('logout');
    }
    public function showLoginForm()
    {
        return view('auth.login.admin');
    }
    public function username()
    {
        return 'username';
    }
    protected function guard()
    {
        return Auth::guard('admin');
    }
}

حالا فرم اصلی لاگین رو duplicate کن در آدرس

resources/views/auth/login/admin.blade.php

در قسمت public function username میتونی فیلدی که به عنوان username میخوای استفاده کنی رو بهش بشناسونی مثلا mobile یا email یا همین username
در فایلی که بالا با اسم admin.blade.php ساختی بجای email فیلد خودت رو جای گذاری کن
تگ های remeber و forget password رو پاک کن.
بعد هم {{ route('login') }} رو به {{ route('admin.login') }} تغییر بده.

روت

Route::prefix('admin')
    ->as('admin.')
    ->group(function() {
        Route::get('home', 'AdminController@index')->name('home');
Route::namespace('Auth\Login')
      ->group(function() {
       Route::get('login', 'AdminController@showLoginForm')->name('login');
    Route::post('login', 'AdminController@login')->name('login');
    Route::post('logout', 'AdminController@logout')->name('logout');
      });
 });

دقت کنید که اگر از لاراول 8 بهره می برید آدرس کنترلر هارو باید کامل وارد کنید یا در RouteServiceProvider مقدار $namespace رو آنکامنت کنید


برای ارسال پاسخ لازم است وارد شده یا ثبت‌نام کنید

ورود یا ثبت‌نام