title: Laravel: General error: 1615 Prepared statement needs to be re-prepared
body: <p>I'm using last version of laravel (5.1) in a homestead virtual machine (vagrant).</p>
  
  <p>I connect my project to a local mariaDB server, in which I have some table and 2 db-view.</p>
  
  <p>Since I made some select only on the db-view tables, I receive back randomly this error:</p>
  
  <blockquote>
    <p>General error: 1615 Prepared statement needs to be re-prepared</p>
  </blockquote>
  
  <p>From today, I always get this error when made select only on the db views.
  If I open my phpMyAdmin and make the same select it return the correct result.</p>
  
  <p>I tried to open <code>php artisan tinker</code> and select one record of the db-view but it return the same error:</p>
  
  <pre><code>// Select one user from user table
  &gt;&gt;&gt; $user = new App\User
  =&gt; &lt;App\User #000000006dc32a890000000129f667d2&gt; {}
  &gt;&gt;&gt; $user = App\User::find(1);
  =&gt; &lt;App\User #000000006dc32a9e0000000129f667d2&gt; {
         id: 1,
         name: "Luca",
         email: "luca@email.it",
         customerId: 1,
         created_at: "2015-08-06 04:17:57",
         updated_at: "2015-08-11 12:39:01"
     }
  &gt;&gt;&gt; 
  // Select one source from Source db-view
  &gt;&gt;&gt; $source = new App\Source
  =&gt; &lt;App\Source #000000006dc32a820000000129f667d2&gt; {}
  &gt;&gt;&gt; $source = App\Source::find(1);
  Illuminate\Database\QueryException with message 'SQLSTATE[HY000]: General error: 1615 Prepared statement needs to be re-prepared (SQL: select * from `sources` where `sources`.`id` = 1 limit 1)'
  </code></pre>
  
  <p>How can I fix that?
  I read about a problem with mysqldump (but not in my case) and to increase value of <code>table_definition_cache</code> but it is not sure that it will work and I can't modify them.</p>
  
  <p>Is this a kind of laravel bug?</p>
  
  <p>How can I figure that out?</p>
  
  <hr>
  
  <p>Edit:</p>
  
  <p>As asked, I add my model source code.
  Source.php:</p>
  
  <pre><code>&lt;?php
  
  namespace App;
  
  use Illuminate\Database\Eloquent\Model;
  
  class Source extends Model
  {
      protected $table = 'sources';
  
  
      /*
      |--------------------------------------------------------------------------
      | FOREIGN KEYS
      |--------------------------------------------------------------------------
      */
  
      /**
       * 
       * @return [type] [description]
       */
      public function customersList(){
          return $this-&gt;hasMany("App\CustomerSource", "sourceId", "id");
      }
  
  
      /**
       * 
       * @return [type] [description]
       */
      public function issues(){
          return $this-&gt;hasMany("App\Issue", "sourceId", "id");
      }
  }
  </code></pre>
  
  <hr>
  
  <p>Edit 2:</p>
  
  <p>If I execute the same query in the project with mysqli it works:</p>
  
  <pre><code>$db = new mysqli(getenv('DB_HOST'), getenv('DB_USERNAME'), getenv('DB_PASSWORD'), getenv('DB_DATABASE'));
  if($db-&gt;connect_errno &gt; 0){
      dd('Unable to connect to database [' . $db-&gt;connect_error . ']');
  }
  $sql = "SELECT * FROM `sources` WHERE `id` = 4";
  if(!$result = $db-&gt;query($sql)){
      dd('There was an error running the query [' . $db-&gt;error . ']');
  }
  
  dd($result-&gt;fetch_assoc());
  </code></pre>
  
  <hr>
  
  <p>EDIT 3:
  Afeter 2 month, I'm still there. Same error and no solution found.
  I decide to try a little solution in aritsan tinker but no good news.
  I report what I've tried:</p>
  
  <p>First try to fetch a table model:</p>
  
  <pre><code>&gt;&gt;&gt; $user = \App\User::find(1);
  =&gt; App\User {#697
       id: 1,
       name: "Luca",
       email: "luca.d@company.it",
       customerId: 1,
       created_at: "2015-08-06 04:17:57",
       updated_at: "2015-10-27 11:28:14",
     }
  </code></pre>
  
  <p>Now try to fetch a view table model:</p>
  
  <pre><code>&gt;&gt;&gt; $ir = \App\ContentRepository::find(15);
  Illuminate\Database\QueryException with message 'SQLSTATE[42S02]: Base table or view not found: 1146 Table 'dbname.content_repositories' doesn't exist (SQL: select * from `content_repositories` where `content_repositories`.`id` = 1 limit 1)'
  </code></pre>
  
  <p>When contentRepository doesn't have correct table name setup inside the model ContentRepository.php:</p>
  
  <pre><code>&gt;&gt;&gt; $pdo = DB::connection()-&gt;getPdo();
  =&gt; PDO {#690
       inTransaction: false,
       errorInfo: [
         "00000",
         1146,
         "Table 'dbname.content_repositories' doesn't exist",
       ],
       attributes: [
         "CASE" =&gt; NATURAL,
         "ERRMODE" =&gt; EXCEPTION,
         "AUTOCOMMIT" =&gt; 1,
         "PERSISTENT" =&gt; false,
         "DRIVER_NAME" =&gt; "mysql",
         "SERVER_INFO" =&gt; "Uptime: 2513397  Threads: 12  Questions: 85115742  Slow queries: 6893568  Opens: 1596  Flush tables: 1  Open tables: 936  Queries per second avg: 33.864",
         "ORACLE_NULLS" =&gt; NATURAL,
         "CLIENT_VERSION" =&gt; "mysqlnd 5.0.11-dev - 20120503 - $Id: id_here $",
         "SERVER_VERSION" =&gt; "5.5.5-10.0.17-MariaDB-1~wheezy-wsrep-log",
         "STATEMENT_CLASS" =&gt; [
           "PDOStatement",
         ],
         "EMULATE_PREPARES" =&gt; 0,
         "CONNECTION_STATUS" =&gt; "localiphere via TCP/IP",
         "DEFAULT_FETCH_MODE" =&gt; BOTH,
       ],
     }
  &gt;&gt;&gt; 
  </code></pre>
  
  <p>CHANGE TABLE VALUE INSIDE model ContentRepository.php:</p>
  
  <pre><code>&gt;&gt;&gt; $ir = \App\ContentRepository::find(15);
  Illuminate\Database\QueryException with message 'SQLSTATE[HY000]: General error: 1615 Prepared statement needs to be re-prepared (SQL: select * from `contentRepository` where `contentRepository`.`id` = 15 limit 1)'
  </code></pre>
  
  <p>When it is correct, <strong>pay attention to "errorInfo" that is missing</strong>:</p>
  
  <pre><code>&gt;&gt;&gt; $pdo = DB::connection()-&gt;getPdo();
  =&gt; PDO {#690
       inTransaction: false,
       attributes: [
         "CASE" =&gt; NATURAL,
         "ERRMODE" =&gt; EXCEPTION,
         "AUTOCOMMIT" =&gt; 1,
         "PERSISTENT" =&gt; false,
         "DRIVER_NAME" =&gt; "mysql",
         "SERVER_INFO" =&gt; "Uptime: 2589441  Threads: 13  Questions: 89348013  Slow queries: 7258017  Opens: 1604  Flush tables: 1  Open tables: 943  Queries per second avg: 34.504",
         "ORACLE_NULLS" =&gt; NATURAL,
         "CLIENT_VERSION" =&gt; "mysqlnd 5.0.11-dev - 20120503 - $Id: id_here $",
         "SERVER_VERSION" =&gt; "5.5.5-10.0.17-MariaDB-1~wheezy-wsrep-log",
         "STATEMENT_CLASS" =&gt; [
           "PDOStatement",
         ],
         "EMULATE_PREPARES" =&gt; 0,
         "CONNECTION_STATUS" =&gt; "localIPhere via TCP/IP",
         "DEFAULT_FETCH_MODE" =&gt; BOTH,
       ],
     }
  </code></pre>
  
  <p>Show db's tables:</p>
  
  <pre><code>&gt;&gt;&gt; $tables = DB::select('SHOW TABLES');
  =&gt; [
       {#702
         +"Tables_in_dbname": "table_name_there",
       },
       {#683
         +"Tables_in_dbname": "table_name_there",
       },
       {#699
         +"Tables_in_dbname": "table_name_there",
       },
       {#701
         +"Tables_in_dbname": "table_name_there-20150917-1159",
       },
       {#704
         +"Tables_in_dbname": "contentRepository", */ VIEW TABLE IS THERE!!!! /*
       },
       {#707
         +"Tables_in_dbname": "table_name_there",
       },
       {#684
         +"Tables_in_dbname": "table_name_there",
       },
     ]
  </code></pre>
  
  <p>Try with normal select:</p>
  
  <pre><code>&gt;&gt;&gt; $results = DB::select('select * from dbname.contentRepository limit 1');
  Illuminate\Database\QueryException with message 'SQLSTATE[HY000]: General error: 1615 Prepared statement needs to be re-prepared (SQL: select * from dbname.contentRepository limit 1)'
  </code></pre>
  
  <p>Try unprepared query:</p>
  
  <pre><code>&gt;&gt;&gt; DB::unprepared('select * from dbname.contentRepository limit 1')
  =&gt; false
  </code></pre>
  
  <p>Try second time unprepared query:</p>
  
  <pre><code>&gt;&gt;&gt; DB::unprepared('select * from dbname.contentRepository limit 1')
  Illuminate\Database\QueryException with message 'SQLSTATE[HY000]: General error: 2014 Cannot execute queries while other unbuffered queries are active.  Consider using PDOStatement::fetchAll().  Alternatively, if your code is only ever going to run against mysql, you may enable query buffering by setting the PDO::MYSQL_ATTR_USE_BUFFERED_QUERY attribute. (SQL: select * from dbname.contentRepository limit 1)'
  </code></pre>
  
  <p>Try PDOStatement::fetchAll():</p>
  
  <pre><code>&gt;&gt;&gt; DB::fetchAll('select * from dbname.contentRepository limit 1'); 
  PHP warning:  call_user_func_array() expects parameter 1 to be a valid callback, class 'Illuminate\Database\MySqlConnection' does not have a method 'fetchAll' in /Users/luca/company/Laravel/dbname/vendor/laravel/framework/src/Illuminate/Database/DatabaseManager.php on line 296
  </code></pre>
  
  <p>Try second PDOStatement::fetchAll():</p>
  
  <pre><code>&gt;&gt;&gt; $pdo::fetchAll('select * from dbname.contentRepository limit 1');
    [Symfony\Component\Debug\Exception\FatalErrorException]  
    Call to undefined method PDO::fetchAll()           
  </code></pre>
  
  <p>Try statement... :</p>
  
  <pre><code>&gt;&gt;&gt; $pdos = DB::statement('select * from dbname.contentRepository limit 1')
  Illuminate\Database\QueryException with message 'SQLSTATE[HY000]: General error: 1615 Prepared statement needs to be re-prepared (SQL: select * from dbname.contentRepository limit 1)'
  </code></pre>
  
  <p>Thank you</p>