title: Laravel - Pass object through view and use in blade
body: <p>Basically, I've a simple  'exp' column in my database and I want to use a function 'showLevel()' where I can pass in <code>Auth::user-&gt;username</code> as the argument instead of a static name. I'm struggling with how I can use either a function in an echo on my template or with an object.</p>
  
  <pre><code>public function index()
  {
      $users = User::all();
  
      $level = $this-&gt;showLevel('test');
  
      return view('home')-&gt;with('users', $users)-&gt;with('showLevel', $level);
  }
  
  public function showLevel($username) {
  
      $level = DB::table('users')-&gt;where('username', $username)-&gt;pluck('exp');
  
      if ($level &lt; 500) {
          return 1;
      } else if ($level &gt; 500 &amp;&amp; $level &lt; 1000) {
          return 2;
      } else if ($level &gt; 1000 &amp;&amp; $level &lt; 1500) {
          return 3;
      } else if ($level &gt; 1500 &amp;&amp; $level &lt; 2000) {
          return 4;
      } else if ($level &gt; 2000 &amp;&amp; $level &lt; 2500) {
          return 5;
      } else if ($level &gt; 2500) {
          return 'MAX';
      }
  
  }
  </code></pre>
  
  <p>I tried creating an object in home.blade.php.</p>
  
  <pre><code>$level = new HomeController;
  $level-&gt;showLevel(Auth::user-&gt;username);
  </code></pre>
  
  <p>But that didn't seem to work. Can you pass objects in blade? I've been trying to look this up myself but maybe I'm wording it wrong. I'm stuck!</p>
  
  <p>I basically want to be able to do something like <code>{{ showLevel(Auth::user-&gt;username); }}</code> in my home.blade.php to echo the level that's returned from the function.</p>