我有一个网站,允许用户发布类似于Facebook的状态,如果用户试图在同一时间发布该数量,我不想让用户每小时发布最多10个帖子,我想用一条消息重定向用户一小时只允许10个帖子。
我发布状态的代码如下所示:
public function postStatus(Request $request, LaravelFacebookSdk $fb, $checked)
{
$checked = $checked;
$twitterToken = Session::get('access_token');
$token = Session::get('fb_user_access_token');
if ($request->has('image_id')) {
$preloaded = new \Cloudinary\PreloadedFile($_POST['image_id']);
if (!$preloaded->is_valid()) {
echo "Invalid upload signature"; //Add a route to page with sorry there was a problem uploading your image.
} else {
//Save Photo URL and User ID to Photo model
$photorequest = $preloaded->pmsFileName();
$user = Auth::user();
$photo = new Photo;
$photo->image_identifier = $photorequest;
$photo->user()->associate($user);
$photo->save();
$regPhoto = Photo::select('id')->where('image_identifier', $preloaded->pmsFileName())->first();
$statusPhotoID = $regPhoto->id;
}
}else{
$statusPhotoID = NULL;
}
$this->validate($request, [
'player_url' => 'url|max:255',
'body' => 'max:1000',
]);
if ($request->has('player_url') || $request->has('body') || $request->has('image_id')) {
Auth::user()->statuses()->create([
'body' => $request->input('body'),
'player_url' => $request->input('player_url'),
'status_photo_id' => $statusPhotoID,
]);
$userPage = ' http://example.com'; //Do something like {{ route('profile.index', [username = Auth::user()->username])}}
$tweetLength = 140 - strlen($userPage);
//Post to Facebook with Image
if ($checked == 'checked' && isset($photorequest)) {
try
{
$fb->setDefaultAccessToken($token);
$fb->post('/me/photos', ['url' => cloudinary_url("$photorequest"),'caption' => $request->input('body') . $userPage]);
}
catch (Facebook\Exceptions\FacebookResponseException $e)
{
// continue to next request;
}
//Post to Fb without Image
} elseif ($checked == 'checked' && !isset($photorequest)) {
try
{
$fb->setDefaultAccessToken($token);
$fb->post('/me/feed', ['message' => $request->input('body') . $userPage]);
}
catch (Facebook\Exceptions\FacebookResponseException $e)
{
// continue to next request;
}
}
//Post to Twitter with Image
if($twitterToken && isset($photorequest)) {
try
{
// Twitter::postTweet(['status' => str_limit($request->input('body') . ' SITE',137), 'format' => 'json']);
$uploaded_media = Twitter::uploadMedia(['media' => file_get_contents(cloudinary_url("$photorequest"))]);
Twitter::postTweet(['status' => str_limit($request->input('body') . $userPage,$tweetLength), 'media_ids' => $uploaded_media->media_id_string]);
}
catch (Exception $e)
{
// continue to next request;
}
//Post to twitter without image
} elseif ($twitterToken && !isset($photorequest)) {
Twitter::postTweet(['status' => str_limit($request->input('body') . $userPage,$tweetLength), 'format' => 'json']);
}
return redirect()->back()->with('info', 'Status Posted.');
}
return redirect()->back()->with('info', 'Error Occured, Nothing Posted.');
}
我的表格如下:
| id | user_id | parent_id | body | created_at | updated_at |
在我的created_at
我正在使用时间戳()
实施我想要做的事情,我最好的选择是什么?
答案 0 :(得分:1)
只需获取当前时间戳并扣除一小时的秒数。然后你可以通过添加像这样的where子句(当然是伪代码)来获取行数
SELECT COUNT(id) FROM posts WHERE created_at >= $timestamp
您要做的就是检查过去一小时内创建的帖子数是否少于十个。