如果数据库记录限制达到 return 到上一页并显示消息

If database record limit reached return to previous page with message

我正在建立我们的会员级别,每个会员级别都对可以保存到数据库中的记录数量有限制。 laracasts said I would want to check the limit against the membership level in an 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 的问题,这不应该让我创建记录,而应该 return 达到限制消息并提示我升级。

只需在每个 'if' 中使用 dd() 并通过获取 1 条额外记录让我知道条件失败的位置。

您没有重定向到观察者。观察者仅用于在数据写入数据库之前拦截数据。

您将在将数据保存到数据库的控制器上重定向。

在你的控制器中: `

function store(){
    if(!$model->save()) redirect()->back();
       return redirect()->to('#url');
}

`