lundi 25 janvier 2016

Laravel ORM - Custom GroupBy Month JSON Response

I currently I have a model like this-

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class UserSlidePhoto extends Model
{
    protected $table="users_slides_photos";
    protected $fillable=['user_id','social_image_id','original_image_name','provider_key','provider_id','thumbnail_name'];

    public $timestamps = true;

    public function users()
    {
        return $this->belongsTo('App\User')->withTimestamps();
    }
}

And a controller like this-

public  function rearrangePhotoMonth(Request $request)
{
    //$ordering = $request->input('ordering');
    $thumbs = url('/uploads/userfiles/thumbs/')."/";
    $images = url('/uploads/userfiles/images/')."/";
    return UserSlidePhoto::where('user_id', Auth::id())
                  ->selectRaw(
                                "id,
                                CONCAT('".$thumbs."',thumbnail_name) AS thumbnail_url,
                                CONCAT('".$images."',original_image_name) AS original_image_url"
                              )
                  ->get();
}

And I am getting JSON response like this-

[
  {
    "id": 2,
    "thumbnail_url": "http:\/\/localhost:8000\/uploads\/userfiles\/thumbs\/2-YgLYcr5DsB3kHjpa7cxcznjiEFdzeF.png",
    "original_image_url": "http:\/\/localhost:8000\/uploads\/userfiles\/images\/2-YgLYcr5DsB3kHjpa7cxcznjiEFdzeF.png"
  },
  {
    "id": 4,
    "thumbnail_url": "http:\/\/localhost:8000\/uploads\/userfiles\/thumbs\/2-A7rlDeFmMybmXNUtcxKyEtzM9TPywq.png",
    "original_image_url": "http:\/\/localhost:8000\/uploads\/userfiles\/images\/2-A7rlDeFmMybmXNUtcxKyEtzM9TPywq.png"
  },
  {
    "id": 1,
    "thumbnail_url": "http:\/\/localhost:8000\/uploads\/userfiles\/thumbs\/2-Xfvn6b8rnE4loZjZOlv14c0FZYVT3A.png",
    "original_image_url": "http:\/\/localhost:8000\/uploads\/userfiles\/images\/2-Xfvn6b8rnE4loZjZOlv14c0FZYVT3A.png"
  },
  {
    "id": 7,
    "thumbnail_url": "http:\/\/localhost:8000\/uploads\/userfiles\/thumbs\/2-aZa54HnAqwY2DPMlSVI2UpbRohVTlY.png",
    "original_image_url": "http:\/\/localhost:8000\/uploads\/userfiles\/images\/2-aZa54HnAqwY2DPMlSVI2UpbRohVTlY.png"
  }
]

But I like to get response like this-

[
  {
    "month": "January 2015",
    "data": {
      {
        "id": 6,
        "thumbnail_url": "http:\/\/localhost:8000\/uploads\/userfiles\/thumbs\/2-WujsXF8iNSuwxQX4QRebKkOqFinlJV.png",
        "original_image_url": "http:\/\/localhost:8000\/uploads\/userfiles\/images\/2-WujsXF8iNSuwxQX4QRebKkOqFinlJV.png"
      },
      {
        "id": 8,
        "thumbnail_url": "http:\/\/localhost:8000\/uploads\/userfiles\/thumbs\/2-VcgQcjoi0iXM2YOqhgt0fYUZf8kwsE.png",
        "original_image_url": "http:\/\/localhost:8000\/uploads\/userfiles\/images\/2-VcgQcjoi0iXM2YOqhgt0fYUZf8kwsE.png"
      },
      {
        "id": 5,
        "thumbnail_url": "http:\/\/localhost:8000\/uploads\/userfiles\/thumbs\/2-r6jxeFfV7nnLrMxO5l88sC7Pt0Xq1A.png",
        "original_image_url": "http:\/\/localhost:8000\/uploads\/userfiles\/images\/2-r6jxeFfV7nnLrMxO5l88sC7Pt0Xq1A.png"
      }
    }
  },
  {
    "month": "March 2015",
    "data": {
      {
        "id": 2,
        "thumbnail_url": "http:\/\/localhost:8000\/uploads\/userfiles\/thumbs\/2-YgLYcr5DsB3kHjpa7cxcznjiEFdzeF.png",
        "original_image_url": "http:\/\/localhost:8000\/uploads\/userfiles\/images\/2-YgLYcr5DsB3kHjpa7cxcznjiEFdzeF.png"
      },
      {
        "id": 9,
        "thumbnail_url": "http:\/\/localhost:8000\/uploads\/userfiles\/thumbs\/2-ivnXQ5ZA1y2mVVxcRtmH3UFd41Xdg3.png",
        "original_image_url": "http:\/\/localhost:8000\/uploads\/userfiles\/images\/2-ivnXQ5ZA1y2mVVxcRtmH3UFd41Xdg3.png"
      }
    }
  },
  {
    "month": "January 2016",
    "data": {
      {
        "id": 3,
        "thumbnail_url": "http:\/\/localhost:8000\/uploads\/userfiles\/thumbs\/2-NfMq71wghF7Al3Fn4KS3SG9ylC4ayo.png",
        "original_image_url": "http:\/\/localhost:8000\/uploads\/userfiles\/images\/2-NfMq71wghF7Al3Fn4KS3SG9ylC4ayo.png"
      }
    }
  }
]

Can anyone please help?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire