Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Route
- Route::name('user.')->prefix('user')->group(function () {
- Route::get('/', 'UserController@index')->name('index');
- });
- // Controller method
- public function index()
- {
- $includes = $this->parseIncludes([
- 'profile',
- 'state',
- 'wallets'
- ]);
- $from = request()->has('from') ? Carbon::parse(request()->query('from')) : null;
- $to = request()->has('to') ? Carbon::parse(request()->query('to')) : null;
- $this->parseOrderBy($this->userRepository, self::ALLOWED_SORT);
- $users = $this->userRepository
- ->with($includes)
- ->paginate(request()->query('limit'), request()->query('offset'))
- ->allRegisteredBetween($brand, $from, $to)->setPath(route('api.user.index'));
- return fractal($users, $this->userTransformer)->parseIncludes($includes);
- }
- // User Transformer
- class UserTransformer extends TransformerAbstract
- {
- /**
- * List of resources possible to include
- *
- * @var array
- */
- protected $availableIncludes = [
- 'profile',
- 'state',
- 'wallet'
- ];
- /**
- * @var ProfileTransformer
- */
- protected $profileTransformer;
- /**
- * @var UserStateTransformer
- */
- protected $userStateTransformer;
- /**
- * @var WalletTransformer
- */
- protected $walletTransformer;
- /**
- * Create a new user transformer instance.
- *
- * @param ProfileTransformer $profileTransformer
- * @param UserStateTransformer $userStateTransformer
- * @param WalletTransformer $walletTransformer
- */
- public function __construct(
- ProfileTransformer $profileTransformer,
- UserStateTransformer $userStateTransformer,
- WalletTransformer $walletTransformer
- ) {
- $this->profileTransformer = $profileTransformer;
- $this->userStateTransformer = $userStateTransformer;
- $this->walletTransformer = $walletTransformer;
- }
- /**
- * A Fractal transformer.
- *
- * @return array
- */
- public function transform($user)
- {
- $visible = [];
- if (Gate::allows('user.view', $user) || request()->route()->hasParameter('email')) {
- $visible[] = 'email';
- $visible[] = 'ip_address_on_registration';
- }
- return ($user) ? array_merge($user->makeVisible($visible)->toArray(), [
- 'referral_code' => Hashids::connection('referral')->encode($user->id)
- ]) : [];
- }
- /**
- * Include profile.
- *
- * @return Item
- */
- public function includeProfile($user)
- {
- return $this->item($user->profile, $this->profileTransformer, false);
- }
- /**
- * Include state.
- *
- * @return Item
- */
- public function includeState($user)
- {
- return $this->item($user->state, $this->userStateTransformer, false);
- }
- /**
- * Include wallet.
- *
- * @return Collection
- */
- public function includeWallet($user)
- {
- return $this->collection($user->wallets, $this->walletTransformer, false);
- }
- }
- // User Repository method, user repository extends AppRepository which has `with`, `paginate` and `execute` methods
- public function allRegisteredBetween(Brand $brand = null, Carbon $from = null, Carbon $to = null)
- {
- $query = User::query();
- if ($brand) {
- $query->where('brand_id', $brand->id);
- }
- if ($from) {
- $query->where('created_at', '>=', $from);
- }
- if ($to) {
- $query->where('created_at', '<=', $to);
- }
- return $this->execute($query);
- }
- // App repository
- class AppRepository
- {
- use UsesRelationships;
- /**
- * @var boolean
- */
- protected $paginate = false;
- /**
- * @var integer
- */
- protected $limit = null;
- /**
- * @var integer
- */
- protected $offset = 1;
- /**
- * @var array
- */
- protected $relations = [];
- /**
- * Specify the repository to paginate the response.
- *
- * @param integer $limit
- * @param integer $offset
- * @return self
- *
- * @throws BindingResolutionException
- */
- public function paginate($limit = null, $offset = 1)
- {
- $offset = ($offset + 1);
- $validator = app()->make('Support\Validators\Pagination\PaginationValidator');
- $validator->validate([
- 'limit' => $limit,
- 'offset' => $offset
- ]);
- $this->paginate = true;
- $this->limit = $limit;
- $this->offset = $offset;
- return $this;
- }
- /**
- * Specify the repository to include relationships.
- *
- * @param array|string $relations
- * @return self
- */
- public function with($relations)
- {
- if (is_string($relations)) {
- $this->relations = explode(',', $relations);
- } else {
- $this->relations = is_array($relations) ? $relations : [];
- }
- return $this;
- }
- /**
- * Execute the specified query including the specified
- * relations and pagination parameters.
- *
- * @param Builder $query
- * @param bool $first
- * @return LengthAwarePaginator|Collection|Model
- */
- protected function execute($query, $first = false)
- {
- $relations = $this->relations();
- if (isset($relations) && count($relations) > 0) {
- $query->with($relations);
- }
- $result = null;
- if (!$first && $this->paginate) {
- if (isset($this->limit) && !is_null($this->limit)) {
- $result = $query->paginate($this->limit, ['*'], 'page', $this->offset);
- } else {
- $result = $query->get();
- $result = new LengthAwarePaginator($result->all(), $result->count(), max($result->count(), 1), $this->offset);
- }
- } else {
- if ($first) {
- $result = $query->first();
- } else {
- $result = $query->get();
- }
- }
- $this->relations = [];
- $this->paginate = false;
- return $result;
- }
- }
- // User Model
- <?php
- class User extends Authenticatable
- {
- use HasApiTokens, Notifiable, LaratrustUserTrait, Authorizable, SoftDeletes {
- Authorizable::can insteadof LaratrustUserTrait;
- }
- /**
- * The attributes that are mass assignable.
- *
- * @var array
- */
- protected $fillable = [
- 'referred_by',
- 'brand_id',
- 'username',
- 'email',
- 'email_verified_at',
- 'email_verification_token',
- 'password',
- 'password_reset_token',
- 'rate_limit',
- 'ip_address_on_registration'
- ];
- /**
- * The attributes that should be hidden for arrays.
- *
- * @var array
- */
- protected $hidden = [
- 'email',
- 'password',
- 'email_verification_token',
- 'password_reset_token',
- 'deleted_at',
- 'updated_at',
- 'rate_limit',
- 'ip_address_on_registration'
- ];
- /**
- * The attributes that should be mutated to dates.
- *
- * @var array
- */
- protected $dates = [
- 'email_verified_at'
- ];
- /**
- * Determine the request rate limit of the user.
- *
- * @return int
- */
- public function getRateLimitAttribute()
- {
- $rateLimit = $this->attributes['rate_limit'];
- return $rateLimit < 0 ? PHP_INT_MAX : $rateLimit;
- }
- /**
- * Set the user's password.
- *
- * @param string $password
- * @return void
- */
- public function setPasswordAttribute($password)
- {
- $this->attributes['password'] = Hash::make($password);
- }
- /**
- * Retrieve the state associated to the user.
- *
- * @return HasOne
- */
- public function state()
- {
- return $this->hasOne('Domain\UserState\UserState');
- }
- /**
- * Retrieve the profile associated to the user.
- *
- * @return HasOne
- */
- public function profile()
- {
- return $this->hasOne('Domain\Profile\Profile');
- }
- /**
- * Retrieve the wallet associated to the user.
- *
- * @return HasMany
- */
- public function wallets()
- {
- return $this->hasMany('Domain\Wallet\Wallet');
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment