title: Eloquent and Laravel, get the MONTH and YEAR of a datetime
body: <p>I have an Eloquent query that is giving me an error with the ´ ´ that Laravel put on it. If I copy-paste the same query as SQL in my database it works perfect, but I don't know hot the proceed with MONTH() and YEAR() correctly in Eloquent. I have this:</p>
  
  <pre><code>$datos = MyModel::select([
  'id',
  'period',
  'MONTH(time_table.period) AS month',
  'YEAR(time_table.period) AS year'])
  -&gt;get();
  </code></pre>
  
  <p>and <code>period</code> is a datetime value. I need to retrieve the month and year of that 'period' value.</p>
  
  <p>What can I do?</p>
  
  <p>EDIT:</p>
  
  <p>The error in the app is (with the original values):</p>
  
  <p>(2/2) QueryException
  SQLSTATE[42S22]: Column not found: 1054 Unknown column 'MONTH(admin_fijacion_anteriores.periodo)' in 'field list' (SQL: select <code>id_fijacion</code> as <code>id</code>, <code>admin_distribuidoras</code>.<code>nombre</code> as <code>nombre_distribuidora</code>, <code>admin_licitaciones</code>.<code>nombre</code> as <code>nombre_licitacion</code>, <code>admin_generadoras</code>.<code>nombre</code> as <code>nombre_generadora</code>, <code>admin_bloques</code>.<code>nombre</code> as <code>nombre_bloques</code>, <code>periodo</code>, <code>energia</code>, <code>potencia</code>, <code>MONTH(admin_fijacion_anteriores</code>.<code>periodo)</code> as <code>mes</code>, <code>YEAR(admin_fijacion_anteriores</code>.<code>periodo)</code> as <code>anio</code> from <code>admin_fijacion_anteriores</code> inner join <code>admin_distribuidoras</code> on <code>admin_fijacion_anteriores</code>.<code>distribuidora_id</code> = <code>admin_distribuidoras</code>.<code>id_distribuidora</code> inner join <code>admin_licitaciones</code> on <code>admin_fijacion_anteriores</code>.<code>licitacion_id</code> = <code>admin_licitaciones</code>.<code>id_licitacion</code> inner join <code>admin_generadoras</code> on <code>admin_fijacion_anteriores</code>.<code>generadora_id</code> = <code>admin_generadoras</code>.<code>id_generadora</code> inner join <code>admin_bloques</code> on <code>admin_fijacion_anteriores</code>.<code>bloque_id</code> = <code>admin_bloques</code>.<code>id_bloque</code>)</p>
  
  <p>and the code of my real Eloquent query is:</p>
  
  <pre><code>$datos = AdminFijacionAnteriore::select([
      'id_fijacion AS id',
      'admin_distribuidoras.nombre as nombre_distribuidora',
      'admin_licitaciones.nombre as nombre_licitacion',
      'admin_generadoras.nombre as nombre_generadora',
      'admin_bloques.nombre as nombre_bloques',
      'periodo',
      'energia',
      'potencia',
      'MONTH(admin_fijacion_anteriores.periodo) AS mes',
      'YEAR(admin_fijacion_anteriores.periodo) AS anio'])
      -&gt;join('admin_distribuidoras', 'admin_fijacion_anteriores.distribuidora_id', '=', 'admin_distribuidoras.id_distribuidora')
      -&gt;join('admin_licitaciones', 'admin_fijacion_anteriores.licitacion_id', '=', 'admin_licitaciones.id_licitacion')
      -&gt;join('admin_generadoras', 'admin_fijacion_anteriores.generadora_id', '=', 'admin_generadoras.id_generadora')
      -&gt;join('admin_bloques', 'admin_fijacion_anteriores.bloque_id', '=', 'admin_bloques.id_bloque')
      -&gt;get();
  </code></pre>