0

I've a problem to retrieve datetime(6) field from MySQL 5.7 table. If I run it directly from mysql client, it works:

mysql> SELECT arrival from table1 limit 1;
+----------------------------+
| arrival                    |
+----------------------------+
| 2016-06-22 16:52:06.260000 |
+----------------------------+
1 row in set (0.00 sec)

mysql>

but getting the field from Laravel using Eloquent, the microsecond are not reported:

class Table1Model extends Model
{
   protected $table = 'table1';
}

class Table1Controller extends Controller
{
   public function index()
   {
       $data = Table1Model::first()->arrival;
       dd($data);
   }
}

// the output is: "2016-06-22 16:52:06"

Is this an Eloquent problem? How get the microsecond?

Thank you.

vlauciani
  • 1,010
  • 2
  • 13
  • 27
  • Try adding `protected $dates = [ "arrival", "created_at", "updated_at" ]` (assuming you also have the other 2) and see if carbon picks it up correctly. – apokryfos Nov 14 '17 at 13:30
  • Thank you @apokryfos but this doesn't work. The output is the same... – vlauciani Nov 15 '17 at 11:03
  • [this bug report](https://bugs.php.net/bug.php?id=54648) looks relevant. It appears that PDO does not support fractional datetimes. The case looks like it's been resolved but I don't know which version it's resolved in. [this answer](https://stackoverflow.com/a/31930762/487813) may have a workaround – apokryfos Nov 15 '17 at 14:44

1 Answers1

0

Thanks @apokryfos , true... this is a bug.

I found a simple workaround using RAW query:

class Table1Model extends Model
{
   protected $table = 'table1';
}

class Table1Controller extends Controller
{
   public function index()
   {
       $query = Table1Model::select(
            DB::raw(
                  CONVERT(CAST(arrival AS DATETIME(3)), CHAR)
            )
       );
       $data = $query->get();
       dd($data);
   }
}
// the output is: "2016-06-22 16:52:06.260"
vlauciani
  • 1,010
  • 2
  • 13
  • 27