Skip to content

Instantly share code, notes, and snippets.

@FrancisBaileyH
Created April 6, 2017 19:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save FrancisBaileyH/cbc6304d563926b81a7eb6cbc9f8c06b to your computer and use it in GitHub Desktop.
Save FrancisBaileyH/cbc6304d563926b81a7eb6cbc9f8c06b to your computer and use it in GitHub Desktop.
<?php
/**
* Fetch the image ids of the most recent images
*/
public function recent($last_request_time = null) {
$photos = null;
$json = [
'success' => 'Most recent images fetched',
'last_request_time' => '',
'results' => []
];
if ($last_request_time != null) {
$photos = Photo::whereRaw('created_at < FROM_UNIXTIME(:lasttime)', array('lasttime' => $last_request_time))
->orderBy('created_at', 'desc')
->take(4)
->get();
}
else {
$photos = Photo::
orderBy('created_at', 'desc')
->take(4)
->get();
}
$last = null;
foreach ($photos as $photo) {
$user = $photo->user()->first();
$json['results'][] = [
'image_id' => $photo->id,
'owner' => $user->id,
'owner_name' => $user->name
];
$last = $photo;
}
if ($last != null) {
$json['last_request_time'] = $last->created_at->timestamp;
}
else {
$json['last_request_time'] = $last_request_time;
}
return response()->json($json, 200);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment