私はメンバーシップレベルを構築しており、それぞれにデータベースに保存できるレコードの数に制限があります。 laracastsの誰かが、会員レベルの制限をobserverにチェックし、その制限に達すると前のページにリダイレクトしてエラーを表示したいと述べました。データベースレコードの上限に達した場合、メッセージ付きで前のページに戻る
これは、リミットが到達したことを示すメッセージでリダイレクトされ、レコードが作成される点を除いて、動作しているようです。理想的には、限界に達するとレコードを作成すべきではありません。私は間違って何をしているのかわからない、私はまたreturn back();
の代わりにreturn false;
にしようとしたが、そのdidnt仕事。
(これを行うための簡単な方法があるかどうかの方法で、私はすべての耳をしています)
ここでは私のオブザーバーである:問題の
<?php
namespace App\Observers;
use App\Company;
use Auth;
use Session;
class SubscriptionLimits {
public function __construct(){
$this->company = Auth::user()->company_id;
}
public function creating()
{
try {
$company = Company::find($this->company);
$clients = \App\Client::where(['company_id' => $this->company])->count();
$contacts = \App\Contact::where(['company_id' => $this->company])->count();
$leads = \App\Lead::where(['company_id' => $this->company])->count();
$opportunities = \App\Opportunity::where(['company_id' => $this->company])->count();
$invoices = \App\Invoice::where(['company_id' => $this->company])->count();
$estimates = \App\Estimate::where(['company_id' => $this->company])->count();
$proposals = \App\Proposal::where(['company_id' => $this->company])->count();
$projects = \App\Project::where(['company_id' => $this->company])->count();
$tasks = \App\Task::where(['company_id' => $this->company])->count();
$boards = \App\Board::where(['company_id' => $this->company])->count();
$bulletins = \App\Bulletin::where(['company_id' => $this->company])->count();
$cards = \App\Card::where(['company_id' => $this->company])->count();
$lineitems = \App\LineItem::where(['company_id' => $this->company])->count();
$notes = \App\Note::where(['company_id' => $this->company])->count();
$timer = \App\Timer::where(['company_id' => $this->company])->count();
$templates = \App\Template::where(['company_id' => $this->company])->count();
$userExtra = \App\UserExtra::where(['company_id' => $this->company])->count();
$count = $clients + $contacts + $leads + $opportunities + $invoices + $estimates + $proposals + $projects + $tasks + $boards + $bulletins + $cards + $lineitems + $notes + $timer + $templates + $userExtra;
if($count >= 5 && !$company->subscriptions){ //Free tier throw error because limit is reached
//return false; //Works but throws error
Session::flash('error', 'You have reached your record limit, please <a href="/order">upgrade now</a> to continue');
return back();
}
if($company->subscribed('middle_tier_monthly_per_user') || $company->subscribed('middle_tier_annual_per_user')){
if($count > 10){ //If middle tier and limit is reached
//return false; //Works but throws error
Session::flash('error', 'You have reached your record limit, please <a href="/order">upgrade now</a> to continue');
return back();
}
}
} catch (Exception $e) {
Session::flash('error', 'You have reached your record limit, please <a href="/order">upgrade now</a> to continue');
return back();
}
}
}
UPDATE Here is a video、これはいけませんレコードを作成し、代わりに限界に達したメッセージを返してアップグレードを促す必要があります。
まずメッセージが異なっている必要があります。エラーが限度に達したが、それが実際には別のものであったと主張すると、誰もが誤解を招いています(データベースがオフラインなど)。 (また、 'use Exception') – apokryfos