2017-06-29 111 views
1

我想讓用戶最多隻能發佈每分鐘1條評論。限制評論

我試過簡單地使用throttle中間件,它不工作。我仍然可以每秒發表評論。

路線代碼:

Route::post('comment/{id}', '[email protected]')->name('comment')->middleware('throttle'); 

控制器代碼:

public function comment($id) 
{ 
    $this->validate(request(), [ 
     "body" => "required", 
    ]); 

    $jersey = Jersey::findOrFail($id); 
    $comment = new Comment; 
    $comment->user_id = auth()->user()->id; 
    $comment->jersey_id = $jersey->id; 
    $comment->body = request()->input('body'); 
    $comment->save(); 
    activity()->by(auth()->user())->withProperties($comment)->log('Commented'); 
    request()->session()->flash('status', 'Comment submitted!'); 

    return redirect()->route('concept', $id); 
} 

如何讓這個它會閃爍而不是保存如果用戶嘗試發佈超過1個評論的錯誤每分鐘?

+0

油門中間件是Laravel的核心功能。 – kjdion84

回答

1

通常我使用油門航線組這樣的:

Route::group(['middleware' => 'throttle:1'], function() { 
    // Your routes here 
    Route::get('/', '[email protected]')->name('comment'); 
    // ... 
)} 

但在你的情況,你可以指定油門參數一樣,修改代碼:

Route::post('comment/{id}', '[email protected]')->name('comment')->middleware('throttle:1'); 

不要忘記清除緩存以應用更改。