0

I am using Firebird and having the following error:

SQLSTATE[IM001]: Driver does not support this function: driver does not support lastInsertId()

I dig a bit on Laravel sources, and found this piece:

public function processInsertGetId(Builder $query, $sql, $values, $sequence = null)
{
    $query->getConnection()->insert($sql, $values);

    $id = $query->getConnection()->getPdo()->lastInsertId($sequence);

    return is_numeric($id) ? (int) $id : $id;

}

I wish to modify the approach to use "INSERT INTO TableName (Field1, Field2) values (Value1, Value2) RETURNING ID" that is the way to get the "last insert id" on Firebird

I dig a little more and found vendor/laravel/framework/src/Illuminate/Query/Processors/PostgresProcessor.php and saw it has a custom implementation for the same function

public function processInsertGetId(Builder $query, $sql, $values, $sequence = null)
{
    $connection = $query->getConnection();

    $connection->recordsHaveBeenModified();

    $result = $connection->selectFromWriteConnection($sql, $values)[0];

    $sequence = $sequence ?: 'id';

    $id = is_object($result) ? $result->{$sequence} : $result[$sequence];

    return is_numeric($id) ? (int) $id : $id;
}

I could not find documentation and samples on how to write custom Processors, I tried to duplicate the Postgres code on another file, searched for other files that references it and made a mimic for Firebird, but without success, I could not find even how to tell that Firebird has a custom processor.

I will be glad if someone has a better approach to solve the problem, or could point me out the documentation where I can learn how to code and configure a custom processor. I have searched a lot and found nothing

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149

1 Answers1

0

There is no native support for Firebird in Laravel. See supported DB engines

However it seems you can try to use dedicated Firebase driver, for example https://github.com/harrygulliford/laravel-firebird to make it possible to use Firebird database in Laravel

Marcin Nabiałek
  • 109,655
  • 42
  • 258
  • 291
  • Thank you for your time. I can access the Firebird database using the mentioned driver, and can read data, the problem is that de Firebird PDO driver does not support lastInsertId used on Insert by Laravel, I was asking if someone know how to fix it or has information/sample/tutorial/documentation on how to write a custom Database Query Processor, so I can write a custom one for Firebird – Alexandre Benson Smith May 17 '23 at 19:38
  • @AlexandreBensonSmith In mentioned package you have good example of https://github.com/harrygulliford/laravel-firebird/blob/main/src/Query/Processors/FirebirdProcessor.php where you could add `processInsertGetId` method and implement it yourself (you could create fork or make pull request to this package). Also you can probably use this trick https://github.com/jacquestvanzuydam/laravel-firebird/issues/25#issuecomment-216392037 to avoid error but not sure if this is what you want to achieve – Marcin Nabiałek May 18 '23 at 05:24