1. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

Using a custom MariaDb Grammar for Carbon timestamp precision with Laravel, the application...

Discussão em 'Outras Linguagens' iniciado por Gear8538, Novembro 8, 2024 às 09:23.

  1. Gear8538

    Gear8538 Guest

    My goal is to store microseconds in a MariaDB database timestamp fields when using a laravel application.

    Following the instructions on https://carbon.nesbot.com/laravel/, I created my Grammar class:


    <?php

    namespace App\Database\Query\Grammars;

    use Illuminate\Database\Query\Grammars\MariaDbGrammar as IlluminateMariaDbGrammar;

    class MariaDbGrammar extends IlluminateMariaDbGrammar
    {
    public function getDateFormat(): string
    {
    return 'Y-m-d H:i:s.u';
    }
    }


    and on my AppServiceProvides.php, I included the line DB::connection()->setQueryGrammar(new \App\Database\Query\Grammars\MariaDbGrammar());

    Then I created a sample artisan command on console.php

    Artisan::command('raw', function () {
    dd(DB::table('user')->where('id_user', 'abc')->toRawSql());
    });


    Running php artisan raw, the application crashes with

    RuntimeException

    The database driver's grammar implementation does not support escaping values.

    at vendor/laravel/framework/src/Illuminate/Database/Grammar.php:238
    [truncated]
    +1 vendor frames

    2 [internal]:0
    Illuminate\Database\Query\Grammars\Grammar::Illuminate\Database\Query\Grammars\{closure}()
    +2 vendor frames

    5 routes/console.php:84
    Illuminate\Database\Query\Builder::toRawSql()


    Commenting out the line, the program works as expected:

    "select * from `user` where `id_user` = 'abc'" // routes/console.php:84


    What is the correct way to enable microseconds for MariaDB databases on a laravel 11 application?

    Continue reading...

Compartilhe esta Página