mardi 6 juin 2017

If database record limit reached return to previous page with message

I am building out our membership levels and each one has limits on how many records one can save to the database. Someone on a laracasts said I would want to check the limit against the membership level in an observer and if the limit has been reached redirect them to the previous page and show an error.

This seems to be working except it redirects with the message that the limit has been reach AND creates the records. Ideally it should not create the record if the limit has been reached. I don't know what I am doing wrong, I also tried to return false; instead of return back(); but that didnt work either.

(by the way if there is an easier way to do this I am all ears)

Here is my observer:

<?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();
            }
    }

}



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire