Advertisement
Guest User

Untitled

a guest
Oct 14th, 2016
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.79 KB | None | 0 0
  1. public function index()
  2. {
  3. $stream = Stream::with('users', 'type','comments', 'comments.users', 'upload', 'likes')->where(function ($query){
  4. $query->where('users_type', 'stream');
  5. })->paginate(2);
  6.  
  7. return $this->response->paginator($stream, new StreamTransformer());
  8. }
  9.  
  10. {
  11. "data": [
  12. {
  13. "stream_id": 2,
  14. "stream_body": "http://www.taringa.net/posts/info/19614066/8-cursos-online-gratuitos-para-mejorar-tu-Ingles.html",
  15. "stream_type": "link",
  16. "stream_user": 1,
  17. "stream_privacy": "public",
  18. "stream_created_at": "hace 3 días",
  19. "stream_hace": 1476134236,
  20. "users": {
  21. "data": {
  22. "user_name": "vdjkelly",
  23. "user_email": "vdjkelly@gmail.com"
  24. }
  25. },
  26. "comments": {
  27. "data": [
  28. {
  29. "comment_body": "dasdasdas",
  30. "comment_stream_id": 2,
  31. "comment_user_id": 1,
  32. "users": {
  33. "data": {
  34. "user_name": "vdjkelly",
  35. "user_email": "vdjkelly@gmail.com"
  36. }
  37. }
  38. },
  39. {
  40. "comment_body": "dsadad",
  41. "comment_stream_id": 2,
  42. "comment_user_id": 1,
  43. "users": {
  44. "data": {
  45. "user_name": "vdjkelly",
  46. "user_email": "vdjkelly@gmail.com"
  47. }
  48. }
  49. },
  50. {
  51. "comment_body": "fdsffs",
  52. "comment_stream_id": 2,
  53. "comment_user_id": 1,
  54. "users": {
  55. "data": {
  56. "user_name": "vdjkelly",
  57. "user_email": "vdjkelly@gmail.com"
  58. }
  59. }
  60. },
  61. {
  62. "comment_body": "sdasdasdas",
  63. "comment_stream_id": 2,
  64. "comment_user_id": 1,
  65. "users": {
  66. "data": {
  67. "user_name": "vdjkelly",
  68. "user_email": "vdjkelly@gmail.com"
  69. }
  70. }
  71. },
  72. {
  73. "comment_body": "sdasdd",
  74. "comment_stream_id": 2,
  75. "comment_user_id": 1,
  76. "users": {
  77. "data": {
  78. "user_name": "vdjkelly",
  79. "user_email": "vdjkelly@gmail.com"
  80. }
  81. }
  82. }
  83. ]
  84. }
  85. }
  86. ],
  87. "meta": {
  88. "pagination": {
  89. "total": 7,
  90. "count": 1,
  91. "per_page": 1,
  92. "current_page": 2,
  93. "total_pages": 7,
  94. "links": {
  95. "previous": "http://timeline.dev/api/home?page=1",
  96. "next": "http://timeline.dev/api/home?page=3"
  97. }
  98. }
  99. }
  100. }
  101.  
  102. class StreamTransformer extends TransformerAbstract
  103. {
  104. protected $availableIncludes = [
  105. 'users', 'comments'
  106. ];
  107.  
  108. protected $defaultIncludes = [
  109. 'users', 'comments'
  110. ];
  111.  
  112. public function transform(Stream $stream)
  113. {
  114.  
  115. return [
  116. 'stream_id' => $stream->id,
  117. 'stream_body' => $stream->body,
  118. 'stream_type' => $stream->stream_type,
  119. 'stream_user' => $stream->user_id,
  120. 'stream_privacy' => $stream->privacy,
  121. 'stream_created_at' => $stream->created_at,
  122. 'stream_hace' => $stream->p_hace
  123. ];
  124. }
  125. public function includeUsers(Stream $stream)
  126. {
  127. $users = $stream->users;
  128. return $this->item($users, new UserTransformer);
  129. }
  130.  
  131. public function includeComments(Stream $stream)
  132. {
  133. $cmt = $stream->comments()->paginate(5);
  134. return $this->collection($cmt, new CommentTransformer());
  135. }
  136.  
  137.  
  138. }
  139.  
  140. "comments": {
  141. "data": [
  142. {
  143. "comment_body": "dasdasdas",
  144. "comment_stream_id": 2,
  145. "comment_user_id": 1,
  146. "users": {
  147. "data": {
  148. "user_name": "vdjkelly",
  149. "user_email": "vdjkelly@gmail.com"
  150. }
  151. }
  152. },
  153. {
  154. "comment_body": "dsadad",
  155. "comment_stream_id": 2,
  156. "comment_user_id": 1,
  157. "users": {
  158. "data": {
  159. "user_name": "vdjkelly",
  160. "user_email": "vdjkelly@gmail.com"
  161. }
  162. }
  163. },
  164. {
  165. "comment_body": "fdsffs",
  166. "comment_stream_id": 2,
  167. "comment_user_id": 1,
  168. "users": {
  169. "data": {
  170. "user_name": "vdjkelly",
  171. "user_email": "vdjkelly@gmail.com"
  172. }
  173. }
  174. },
  175.  
  176. ]
  177. "meta": {
  178. "pagination": {
  179. "total": 9,
  180. "count": 1,
  181. "per_page": 1,
  182. "current_page": 2,
  183. "total_pages": 9,
  184. "links": {
  185. "previous": "http://timeline.dev/api/home?stream_id=2&comment_include&page=1",
  186. "next": "http://timeline.dev/api/home?stream_id=2&comment_include&page=2"
  187. }
  188. }
  189. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement