Laravel ORM - Custom GroupBy Month JSON Response


Laravel ORM - Custom GroupBy Month JSON Response

我目前有一个这样的模型-

<?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();
    }
}

还有这样的控制器——

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(
                                "
                                DATE_FORMAT(created_at, '%M, %Y') as month,
                                id,
                                CONCAT('".$thumbs."',thumbnail_name) AS thumbnail_url,
                                CONCAT('".$images."',original_image_name) AS original_image_url"
                              )
                  ->groupBy('month')
                  ->orderBy('month', 'desc')
                  ->get();
}

我收到这样的 JSON 响应-

[
  {
    "month": "November, 2019",
    "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"
  },
  {
    "month": "November, 2017",
    "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"
  },
  {
    "month": "November, 2016",
    "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"
  },
  {
    "month": "November, 2011",
    "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"
  },
  {
    "month": "November, 2009",
    "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": "November, 2008",
    "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": "March, 1996",
    "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"
  }
]

但我喜欢得到这样的回应——

[
  {
    "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"
      }
    }
  }
]

谁能帮忙?

您有错误的查询,只需运行它即可查看它每月仅返回一行。

所以你需要删除group_by子句并在PHP中手动完成或使用laravel集合

我是这样解决的——

public  function rearrangePhotoMonth(Request $request)
{
    //$ordering = $request->input('ordering');
    $thumbs = url('/uploads/userfiles/thumbs/')."/";
    $images = url('/uploads/userfiles/images/')."/";
    $collection = UserSlidePhoto::where('user_id', Auth::id())
                  ->selectRaw(
                                "
                                DATE_FORMAT(created_at, '%M, %Y') as month,
                                id,
                                CONCAT('".$thumbs."',thumbnail_name) AS thumbnail_url,
                                CONCAT('".$images."',original_image_name) AS original_image_url"
                              )
                  ->orderBy('created_at', 'desc')
                  ->get();
    return collect($collection)
            ->groupBy('month')
            ->all();
}

它现在正在:)工作。

group by每个月只会给你一行。

除非有一些我不知道的雄辩魔法(这很可能是真的),否则你必须通过PHP解析数组来组织你的数据。