Support the ongoing development of Laravel.io →
Database Eloquent

I have one particular Eloquent model that is giving us seemingly random problems with inserting. That particular model's table (MSSQL 2005 using PDO and SqlSrv driver) has a trigger that takes inserts/updates for that table, and adds them to a secondary change queue table so they can be later sync'd with a different system. The problem is, near as we can tell, completely random.... sometimes it works perfectly, other times it fails. When it fails, the model's new ID is reported incorrectly (it's receiving the ID number from the secondary table, not the actual model table) and the model itself is not actually inserted.

I've traced back what I can as far as what's actually going on under the hood when I do a $model->save(), and my database guy seems to think that it's because the function that retreives the new ID value is selecting IDENTITY instead of SCOPE_IDENTITY or something. I think this is PDO::lastInsertId(), so I'm not really even sure how much control I or Laravel would have over this.

Anyway, if anyone else has seen this and come up with a solution, or has any other ideas, I would greatly appreciate it. My only idea right now is to skip Eloquent for this particular model, so that I can directly control the insert and ID-grabbing queries myself. But that seems like a pretty drastic step, I would rather not go that route if there's some other way to make it play nicely with this trigger.

Thanks in advance.

Last updated 3 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.