mercredi 27 juin 2018

how do I inject user so I do not have to look him up in Laravel request

I am following a tutorial on laravel signed routes:

https://dev.to/fwartner/laravel-56---user-activation-with-signed-routes--notifications-oaa

To create the signed route author does this:

$url = URL::signedRoute('activate-email', ['user' => $this->user->id]);

notice that to the 'user' he only assignes the id...

Later when user in question clicks the generated link and another part of code does this:

Route::get('/activate-email/{user}', function (Request $request) {
    if (!$request->hasValidSignature()) {
        abort(401, 'This link is not valid.');
    }

    $request->user()->update([
        'is_activated' => true
    ]);

    return 'Your account is now activated!';
})->name('activate-email');

I am confused by this part:

    $request->user()->update([
        'is_activated' => true
    ]);

Author accesses user() directly and runs update on him?

When I in my own code tried this:

dd($request->user()) I got null and if I tried dd($request->user) I got user id number.

How is author able to call user()->update without looking up the user. I do not see where he does inject the User object, so it seems like magic to me or perhaps author did not test his own code fully, which is on github: https://github.com/fwartner/laravel-user-activation

So how do I inject the user into route so I do not have to explicitly look him up with

$user = User::find($request->user);



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire