public function apply($code)
    {
        if (!auth()->check()) {
            throw new UnauthenticatedException;
        }

        try {
            if ($promocode = $this->check($code)) {
                if ($this->isSecondUsageAttempt($promocode)) {
                    throw new AlreadyUsedException;
                }

                $promocode->users()->attach(auth()->id(), [
                    'promocode_id' => $promocode->id,
                    'used_at' => Carbon::now(),
                ]);

                return $promocode->load('users');
            }
        } catch (InvalidPromocodeException $exception) {
            //
        }

        return false;
    }
public function apply($code)
    {
        if (!auth()->check()) {
            throw new UnauthenticatedException;
        }

        try {
            if ($promocode = $this->check($code)) {
                if ($this->isSecondUsageAttempt($promocode)) {
                    throw new AlreadyUsedException;
                }

                $promocode->users()->attach(auth()->id(), [
                    'promocode_id' => $promocode->id,
                    'used_at' => Carbon::now(),
                ]);

                return $promocode->load('users');
            }
        } catch (InvalidPromocodeException $exception) {
            //
        }

        return false;
    }
public function apply ( $ code ) { if ( ! auth ( ) -> check ( ) ) { throw new UnauthenticatedException ; } try { if ( $ promocode = $ this -> check ( $ code ) ) { if ( $ this -> isSecondUsageAttempt ( $ promocode ) ) { throw new AlreadyUsedException ; } $ promocode -> users ( ) -> attach ( auth ( ) -> id ( ) , [ 'promocode_id' => $ promocode -> id , 'used_at' => Carbon :: now ( ) , ] ) ; return $ promocode -> load ( 'users' ) ; } } catch ( InvalidPromocodeException $ exception ) { // } return false ; }
Apply promocode to user that it's used from now.

@param string $code

@return bool|Promocode
@throws AlreadyUsedException
@throws UnauthenticatedException
Apply promocode to user that it s used from now .