public function applyCode($code, $callback = null)
    {
        try {
            if ($promocode = Promocodes::check($code)) {
                if ($promocode->users()->wherePivot(config('promocodes.related_pivot_key'), $this->id)->exists()) {
                    throw new AlreadyUsedException;
                }

                $promocode->users()->attach($this->id, [
                    config('promocodes.foreign_pivot_key') => $promocode->id,
                    'used_at' => Carbon::now(),
                ]);

                $promocode->load('users');

                if (is_callable($callback)) {
                    $callback($promocode);
                }

                return $promocode;
            }
        } catch (InvalidPromocodeException $exception) {
            //
        }

        if (is_callable($callback)) {
            $callback(null);
        }

        return null;
    }
public function applyCode($code, $callback = null)
    {
        try {
            if ($promocode = Promocodes::check($code)) {
                if ($promocode->users()->wherePivot(config('promocodes.related_pivot_key'), $this->id)->exists()) {
                    throw new AlreadyUsedException;
                }

                $promocode->users()->attach($this->id, [
                    config('promocodes.foreign_pivot_key') => $promocode->id,
                    'used_at' => Carbon::now(),
                ]);

                $promocode->load('users');

                if (is_callable($callback)) {
                    $callback($promocode);
                }

                return $promocode;
            }
        } catch (InvalidPromocodeException $exception) {
            //
        }

        if (is_callable($callback)) {
            $callback(null);
        }

        return null;
    }
public function applyCode ( $ code , $ callback = null ) { try { if ( $ promocode = Promocodes :: check ( $ code ) ) { if ( $ promocode -> users ( ) -> wherePivot ( config ( 'promocodes.related_pivot_key' ) , $ this -> id ) -> exists ( ) ) { throw new AlreadyUsedException ; } $ promocode -> users ( ) -> attach ( $ this -> id , [ config ( 'promocodes.foreign_pivot_key' ) => $ promocode -> id , 'used_at' => Carbon :: now ( ) , ] ) ; $ promocode -> load ( 'users' ) ; if ( is_callable ( $ callback ) ) { $ callback ( $ promocode ) ; } return $ promocode ; } } catch ( InvalidPromocodeException $ exception ) { // } if ( is_callable ( $ callback ) ) { $ callback ( null ) ; } return null ; }
Apply promocode to user and get callback.

@param string $code
@param null|\Closure $callback

@return null|\Gabievi\Promocodes\Model\Promocode
@throws AlreadyUsedException
Apply promocode to user and get callback .