title: Laravel/Eloquent: Fatal error: Call to a member function connection() on a non-object
body: <p>I'm building a package in Laravel 4 but am getting a non-object error when attempting to access the db from which seems to be a properly instantiated object. Here's the setup:</p>
  
  <p>The config and class in question:</p>
  
  <p><strong>composer.json:</strong></p>
  
  <pre><code>...
  "autoload": {
          "classmap": [
              "app/commands",
              "app/controllers",
              "app/models",
              "app/database/migrations",
              "app/database/seeds",
              "app/tests/TestCase.php"
          ],
          "psr-0": {
              "Vendor\\Chat": "src/vendor/chat/src"
          }  
      }
  ...
  </code></pre>
  
  <p><strong>The class:</strong></p>
  
  <pre><code>namespace Vendor\Chat;
  
  use Illuminate\Database\Eloquent\Model as Eloquent;
  
  
  class ChatHistory extends Eloquent
  {
      protected $table = 'chat_history';
  
      protected $fillable = array('message', 'user_id', 'room_token');
  
      public function __construct($attributes = array())
      {
          parent::__construct($attributes);
      }
  
  }
  </code></pre>
  
  <p><strong>The call:</strong></p>
  
  <pre><code>$message = new Message($msg);
  
  $history = new ChatHistory;
  $history-&gt;create(array(
                   'room_token' =&gt; $message-&gt;getRoomToken(),
                   'user_id' =&gt; $message-&gt;getUserId(),
                   'message' =&gt; $message-&gt;getMessage(),
                ));
  </code></pre>
  
  <p><strong>The error:</strong></p>
  
  <pre><code>PHP Fatal error:  Call to a member function connection() on a non-object in /home/vagrant/project/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php on line 2894
  </code></pre>
  
  <p>I believe I'm missing something fundamental and under my nose. Thanks for any and all help!</p>
  
  <p>EDIT:</p>
  
  <p>Here is the class that's instantiating ChatHistory and calling the write:</p>
  
  <pre><code>namespace Vendor\Chat;
  
  use Ratchet\MessageComponentInterface;
  use Ratchet\ConnectionInterface;
  
  use Vendor\Chat\Client;
  use Vendor\Chat\Message;
  use Vendor\Chat\ChatHistory;
  
  use Illuminate\Database\Model;
  
  class Chat implements MessageComponentInterface {
  
      protected $app;
  
      protected $clients;
  
      public function __construct() 
      {
          $this-&gt;clients = new \SplObjectStorage;
      }
  
      public function onOpen(ConnectionInterface $conn) 
      {
          $client = new Client;
          $client-&gt;setId($conn-&gt;resourceId);
          $client-&gt;setSocket($conn);
  
          $this-&gt;clients-&gt;attach($client);
      }
  
      public function onMessage(ConnectionInterface $conn, $msg) 
      {
          $message = new Message($msg);
  
          $history = new ChatHistory;
          ChatHistory::create(array(
                       'room_token' =&gt; $message-&gt;getRoomToken(),
                       'user_id' =&gt; $message-&gt;getUserId(),
                       'message' =&gt; $message-&gt;getMessage(),
                    ));
          /* error here */
          /* ... */ 
      }
  
      public function onClose(ConnectionInterface $conn) 
      {
          $this-&gt;clients-&gt;detach($conn);
      }
  
      public function onError(ConnectionInterface $conn, \Exception $e) 
      {
          $conn-&gt;close();
      }
  
      protected function getClientByConn(ConnectionInterface $conn)
      {
          foreach($this-&gt;clients as $client) {
              if($client-&gt;getSocket() === $conn) {
                  return $client;
              } 
          } 
  
          return null;
      }
  }
  </code></pre>
  
  <p>The fact that DB isn't available suggest that Eloquent isn't being loaded up top?</p>