我目前有一个这样的模型 -
<?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"
}
}
}
]
有人可以帮忙吗?
答案 0 :(得分:1)
您的查询错误,只需运行它以查看每个月只返回一行。
所以你需要删除 group_by 子句并在PHP中手动执行或使用laravel collections
答案 1 :(得分:1)
我已经这样解决了 -
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();
}
现在正在运作:)。
答案 2 :(得分:0)
group by
每月只会给你一行。
除非有一些雄辩的魔法我还不知道(这很可能是真的),你必须通过PHP解析数组以组织你的数据。