Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

xgrave's avatar

How to drop database name in Eloquent Model Query?

Hey all, I am cant get my eloquent queries to work with in memory sqlite unit tests, how can I drop the db name from all model queries?

For Example Item::whereId(1)->toSql() returns "select * from ":memory:"."items" where "id" = ?"

I want to drop the ":memory:". part because it breaks the query.

DB::table('items')->whereId(1)->toSql() returns "select * from "items" where "id" = ?" and works perfectly fine, but I dont want to rewrite every eloquent query into a DB::table one

0 likes
2 replies
martinbean's avatar

@xgrave Eloquent should work with an in-memory database just fine. I personally use them in my tests.

xgrave's avatar

I have tried it on multiple projects, it doesnt work at all for me. Migrations succeed, but as soon as I try to put eloquent queries into the unit tests or the migrations, it says table not found. Do you mind giving me a raw sql dump of one of your eloquent queries in your unit tests? Forgot to mention, i am using RefreshDatabases in my TestCase class

Please or to participate in this conversation.