title: Laravel - Calling member function of class using $this keyword in controller
body: <p>I have an Event class in Laravel as Controller class. Here is the namespace. </p>
  
  <pre><code>namespace App\Http\Controllers\Admin;
  </code></pre>
  
  <p>This is the class starting code and constructor. </p>
  
  <pre><code>class EventController extends Controller
  {
      /**
       * Create a new controller instance.
       *
       * @return void
       */
  
      public function __construct()
      {
          $this-&gt;middleware('auth');
      }
  </code></pre>
  
  <p>And here is the function name and definition </p>
  
  <pre><code>    function generateBarcodeNumber() {
      $number = mt_rand(1000000000, 9999999999); // better than rand()
  
      // call the same function if the barcode exists already
      if (barcodeNumberExists($number)) {
          return generateBarcodeNumber();
      }
  
      // otherwise, it's valid and can be used
      return $number;
  }
  
  function barcodeNumberExists($number) {
      // query the database and return a boolean
      // for instance, it might look like this in Laravel
      return User::whereBarcodeNumber($number)-&gt;exists();
  }
  </code></pre>
  
  <p>I am calling this function in another function using <code>$this</code> keyword as</p>
  
  <pre><code>$event-&gt;slug_str = $this-&gt;generateBarcodeNumber();
  </code></pre>
  
  <p>And this is the error:</p>
  
  <blockquote>
    <p>Call to undefined function App\Http\Controllers\Admin\barcodeNumberExists()</p>
  </blockquote>
  
  <p>Thanks!</p>