Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.49 KB | None | 0 0
  1. <?php
  2.  
  3. namespace App\JsonApi\v1\Comments;
  4.  
  5. use App\Comment;
  6. use App\JsonApi\v1\CustomStrategy;
  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. 'user_id', 'post_id', 'body'
  30. ];
  31.  
  32. protected $defaultWith = [
  33. 'commentator'
  34. ];
  35.  
  36. protected $defaultPagination = [
  37. 'number' => 1
  38. ];
  39.  
  40. protected $searchable = [
  41. 'body'
  42. ];
  43.  
  44. /**
  45. * Adapter constructor.
  46. *
  47. * @param CustomStrategy $paging
  48. */
  49. public function __construct(CustomStrategy $paging)
  50. {
  51. parent::__construct(new Comment, $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('body'));
  62.  
  63. if ($term = $filters->get('search')) {
  64. $query->whereLike($this->searchable, $term);
  65. }
  66. }
  67.  
  68. protected function commentator()
  69. {
  70. return $this->belongsTo();
  71. }
  72.  
  73. protected function post()
  74. {
  75. return $this->belongsTo();
  76. }
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement