T
T
Tikhon Ermakov2020-07-28 22:30:34
Laravel
Tikhon Ermakov, 2020-07-28 22:30:34

What does Guard do and what is it for?

What is the difference between auth()->guard()->user()and auth()->user()?

Also auth()->attempt(...)_auth()->guard()->attempt(...)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton October, 2020-07-29
@the_goldmayer

You can read about what you are looking for in off. documentation or in translation
Clippings from the translation:
Guards, "guards", "guards". These are essentially user authentication rules - in which parts of the request to store information that this request comes from an authenticated user. For example, this can be done in a session/cookie, or in some token that must be contained in every request. In Laravel, these are session and token guards, respectively.
You can also assign a specific guard to handle the authentication process. To do this, create a guard property in your AuthController class. The value of this property should be the name of one of the guards you defined in the config/auth.php file.
protected $guard = 'admin';
You can explicitly specify which guard to serve the authorization process with. This will allow you to have several parts in the application, which are entered according to their own rules. The user may be logged into one of them, or several. The simplest example is the admin panel. Your admin guard determines whether a given user is logged in as admin or not - for example, by setting a special cookie.
Then when logging into the admin panel, you do this:

if (Auth::guard('admin')->attempt($credentials)) {
    //
}

You can also explicitly specify a guard with which to capture the authentication process. You can also create your own custom guard, read about it here
Auth::guard('admin')->login($user);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question