title: If database record limit reached return to previous page with message
body: <p>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 <a href="https://laracasts.com/discuss/channels/eloquent/does-laravel-or-eloquent-provide-an-easy-way-to-count-rows-across-all-tables" rel="nofollow noreferrer">laracasts</a> said I would want to check the limit against the membership level in an <a href="https://laravel.com/docs/5.3/eloquent#observers" rel="nofollow noreferrer">observer</a> and if the limit has been reached redirect them to the previous page and show an error.  </p>
  
  <p>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 <code>return false;</code> instead of <code>return back();</code> but that didnt work either.</p>
  
  <p>(by the way if there is an easier way to do this I am all ears)</p>
  
  <p>Here is my observer:</p>
  
  <pre><code>&lt;?php 
  
  namespace App\Observers;
  
  use App\Company;
  use Auth;
  use Session;
  
  class SubscriptionLimits {
  
      public function __construct(){
          $this-&gt;company =  Auth::user()-&gt;company_id;
      }
  
      public function creating()
      {
        try {
                  $company = Company::find($this-&gt;company);
  
                  $clients = \App\Client::where(['company_id' =&gt; $this-&gt;company])-&gt;count();
                  $contacts = \App\Contact::where(['company_id' =&gt; $this-&gt;company])-&gt;count();
                  $leads = \App\Lead::where(['company_id' =&gt; $this-&gt;company])-&gt;count();
                  $opportunities = \App\Opportunity::where(['company_id' =&gt; $this-&gt;company])-&gt;count();
                  $invoices = \App\Invoice::where(['company_id' =&gt; $this-&gt;company])-&gt;count();
                  $estimates = \App\Estimate::where(['company_id' =&gt; $this-&gt;company])-&gt;count();
                  $proposals = \App\Proposal::where(['company_id' =&gt; $this-&gt;company])-&gt;count();
                  $projects = \App\Project::where(['company_id' =&gt; $this-&gt;company])-&gt;count();
                  $tasks = \App\Task::where(['company_id' =&gt; $this-&gt;company])-&gt;count();
                  $boards = \App\Board::where(['company_id' =&gt; $this-&gt;company])-&gt;count();
                  $bulletins = \App\Bulletin::where(['company_id' =&gt; $this-&gt;company])-&gt;count();
                  $cards = \App\Card::where(['company_id' =&gt; $this-&gt;company])-&gt;count();
                  $lineitems = \App\LineItem::where(['company_id' =&gt; $this-&gt;company])-&gt;count();
                  $notes = \App\Note::where(['company_id' =&gt; $this-&gt;company])-&gt;count();
                  $timer = \App\Timer::where(['company_id' =&gt; $this-&gt;company])-&gt;count();
                  $templates = \App\Template::where(['company_id' =&gt; $this-&gt;company])-&gt;count();
                  $userExtra = \App\UserExtra::where(['company_id' =&gt; $this-&gt;company])-&gt;count();
  
                  $count = $clients + $contacts + $leads + $opportunities + $invoices + $estimates + $proposals + $projects + $tasks + $boards + $bulletins + $cards + $lineitems + $notes + $timer + $templates + $userExtra;
  
                  if($count &gt;= 5 &amp;&amp; !$company-&gt;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 &lt;a href="/order"&gt;upgrade now&lt;/a&gt; to continue');
                      return back();
                  }
  
                  if($company-&gt;subscribed('middle_tier_monthly_per_user') || $company-&gt;subscribed('middle_tier_annual_per_user')){
                      if($count &gt; 10){ //If middle tier and limit is reached
                          //return false; //Works but throws error
                          Session::flash('error', 'You have reached your record limit, please &lt;a href="/order"&gt;upgrade now&lt;/a&gt; to continue');
                          return back();
                      }
                  }
              } catch (Exception $e) {
                  Session::flash('error', 'You have reached your record limit, please &lt;a href="/order"&gt;upgrade now&lt;/a&gt; to continue');
                  return back();
              }
      }
  
  }
  </code></pre>
  
  <p><strong>UPDATE</strong>
  <a href="https://youtu.be/rdJQFmGGEvk" rel="nofollow noreferrer">Here is a video</a> of the issue, this should not let me create a record and should instead return the limit reached message and prompt me to upgrade.</p>