Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public function index()
- {
- $stream = Stream::with('users', 'type','comments', 'comments.users', 'upload', 'likes')->where(function ($query){
- $query->where('users_type', 'stream');
- })->paginate(2);
- return $this->response->paginator($stream, new StreamTransformer());
- }
- {
- "data": [
- {
- "stream_id": 2,
- "stream_body": "http://www.taringa.net/posts/info/19614066/8-cursos-online-gratuitos-para-mejorar-tu-Ingles.html",
- "stream_type": "link",
- "stream_user": 1,
- "stream_privacy": "public",
- "stream_created_at": "hace 3 días",
- "stream_hace": 1476134236,
- "users": {
- "data": {
- "user_name": "vdjkelly",
- "user_email": "vdjkelly@gmail.com"
- }
- },
- "comments": {
- "data": [
- {
- "comment_body": "dasdasdas",
- "comment_stream_id": 2,
- "comment_user_id": 1,
- "users": {
- "data": {
- "user_name": "vdjkelly",
- "user_email": "vdjkelly@gmail.com"
- }
- }
- },
- {
- "comment_body": "dsadad",
- "comment_stream_id": 2,
- "comment_user_id": 1,
- "users": {
- "data": {
- "user_name": "vdjkelly",
- "user_email": "vdjkelly@gmail.com"
- }
- }
- },
- {
- "comment_body": "fdsffs",
- "comment_stream_id": 2,
- "comment_user_id": 1,
- "users": {
- "data": {
- "user_name": "vdjkelly",
- "user_email": "vdjkelly@gmail.com"
- }
- }
- },
- {
- "comment_body": "sdasdasdas",
- "comment_stream_id": 2,
- "comment_user_id": 1,
- "users": {
- "data": {
- "user_name": "vdjkelly",
- "user_email": "vdjkelly@gmail.com"
- }
- }
- },
- {
- "comment_body": "sdasdd",
- "comment_stream_id": 2,
- "comment_user_id": 1,
- "users": {
- "data": {
- "user_name": "vdjkelly",
- "user_email": "vdjkelly@gmail.com"
- }
- }
- }
- ]
- }
- }
- ],
- "meta": {
- "pagination": {
- "total": 7,
- "count": 1,
- "per_page": 1,
- "current_page": 2,
- "total_pages": 7,
- "links": {
- "previous": "http://timeline.dev/api/home?page=1",
- "next": "http://timeline.dev/api/home?page=3"
- }
- }
- }
- }
- class StreamTransformer extends TransformerAbstract
- {
- protected $availableIncludes = [
- 'users', 'comments'
- ];
- protected $defaultIncludes = [
- 'users', 'comments'
- ];
- public function transform(Stream $stream)
- {
- return [
- 'stream_id' => $stream->id,
- 'stream_body' => $stream->body,
- 'stream_type' => $stream->stream_type,
- 'stream_user' => $stream->user_id,
- 'stream_privacy' => $stream->privacy,
- 'stream_created_at' => $stream->created_at,
- 'stream_hace' => $stream->p_hace
- ];
- }
- public function includeUsers(Stream $stream)
- {
- $users = $stream->users;
- return $this->item($users, new UserTransformer);
- }
- public function includeComments(Stream $stream)
- {
- $cmt = $stream->comments()->paginate(5);
- return $this->collection($cmt, new CommentTransformer());
- }
- }
- "comments": {
- "data": [
- {
- "comment_body": "dasdasdas",
- "comment_stream_id": 2,
- "comment_user_id": 1,
- "users": {
- "data": {
- "user_name": "vdjkelly",
- "user_email": "vdjkelly@gmail.com"
- }
- }
- },
- {
- "comment_body": "dsadad",
- "comment_stream_id": 2,
- "comment_user_id": 1,
- "users": {
- "data": {
- "user_name": "vdjkelly",
- "user_email": "vdjkelly@gmail.com"
- }
- }
- },
- {
- "comment_body": "fdsffs",
- "comment_stream_id": 2,
- "comment_user_id": 1,
- "users": {
- "data": {
- "user_name": "vdjkelly",
- "user_email": "vdjkelly@gmail.com"
- }
- }
- },
- ]
- "meta": {
- "pagination": {
- "total": 9,
- "count": 1,
- "per_page": 1,
- "current_page": 2,
- "total_pages": 9,
- "links": {
- "previous": "http://timeline.dev/api/home?stream_id=2&comment_include&page=1",
- "next": "http://timeline.dev/api/home?stream_id=2&comment_include&page=2"
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement