我正在构建会员级别,每个级别都限制了可以保存到数据库的记录数。 laracasts上有人说我想检查observer中会员级别的限制,如果达到限制,则将其重定向到上一页并显示错误。
这似乎有效,除非它重定向到已达到限制的消息并创建记录。理想情况下,如果达到限制,则不应创建记录。我不知道我做错了什么,我也尝试return false;
而不是return back();
,但这也没有用。
(顺便说一句,如果有一种更简单的方法可以做到这一点,我很满意)
以下是我的观察员:
<?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();
}
}
}
更新 Here is a video问题,这不应该让我创建一条记录,而应该返回达到限制的消息并提示我升级。
答案 0 :(得分:2)
你没有重定向观察者。观察者只是在将数据写入数据库之前拦截数据。
您将重定向到您将数据保存到数据库的控制器上。
在你的控制器中: `
ThisWorkbook.ExportAsFixedFormat Type:=xlTypePDF, Filename:= "C:\Place\Name.pdf", _
Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas :=False, _
OpenAfterPublish:=False
`
答案 1 :(得分:0)
每个'if'中只有dd(),并通过额外记录1让我知道条件失败的地方。