SHARE
TWEET

Untitled

a guest Oct 23rd, 2019 75 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2.  
  3. namespace App\JsonApi\v1\Users;
  4.  
  5. use App\JsonApi\v1\CustomStrategy;
  6. use App\User;
  7. use CloudCreativity\LaravelJsonApi\Eloquent\AbstractAdapter;
  8. use Illuminate\Database\Eloquent\Builder;
  9. use Illuminate\Support\Collection;
  10.  
  11. class Adapter extends AbstractAdapter
  12. {
  13.  
  14.     /**
  15.      * Mapping of JSON API attribute field names to model keys.
  16.      *
  17.      * @var array
  18.      */
  19.     protected $attributes = [];
  20.  
  21.     /**
  22.      * Mapping of JSON API filter names to model scopes.
  23.      *
  24.      * @var array
  25.      */
  26.     protected $filterScopes = [];
  27.  
  28.     protected $fillable = [
  29.         'role_id', 'name', 'email', 'password'
  30.     ];
  31.  
  32.     protected $defaultPagination = [
  33.         'number' => 1
  34.     ];
  35.  
  36.     protected $defaultWith = [
  37.         'role'
  38.     ];
  39.  
  40.     protected $searchable = [
  41.         'name', 'email'
  42.     ];
  43.  
  44.     /**
  45.      * Adapter constructor.
  46.      *
  47.      * @param  CustomStrategy  $paging
  48.      */
  49.     public function __construct(CustomStrategy $paging)
  50.     {
  51.         parent::__construct(new User, $paging);
  52.     }
  53.  
  54.     /**
  55.      * @param Builder $query
  56.      * @param Collection $filters
  57.      * @return void
  58.      */
  59.     protected function filter($query, Collection $filters)
  60.     {
  61.         $this->filterWithScopes($query, $filters->only('name', 'email'));
  62.  
  63.         if ($term = $filters->get('search')) {
  64.             $query->whereLike($this->searchable, $term);
  65.         }
  66.     }
  67.  
  68.     protected function role()
  69.     {
  70.         return $this->belongsTo();
  71.     }
  72.  
  73.     protected function posts()
  74.     {
  75.         return $this->hasMany();
  76.     }
  77.  
  78.     protected function comments()
  79.     {
  80.         return $this->hasMany();
  81.     }
  82. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top