In-Memory Eloquent Models with Truffle

Last updated on by

In-Memory Eloquent Models with Truffle image

Truffle, by Waad Mawlood, is a Laravel package that allows Eloquent models to use an in-memory SQLite database instead of your primary database. This approach works for handling static data and reference tables—such as countries, currencies, or roles—that you want to query using Eloquent without the overhead of database migrations or storage in your main database.

By using the Truffle trait, you can define data directly in the model or load it from external files while still using Eloquent's query builder, relationships, and attributes.

Defining In-Memory Records

The simplest way to use Truffle is by defining a $records array directly in your model. Truffle creates a temporary SQLite table and populates it when the model is first accessed:

use Waad\Truffle\Truffle;
use Illuminate\Database\Eloquent\Model;
 
class Country extends Model
{
use Truffle;
 
protected $records = [
['id' => 1, 'name' => 'United States', 'code' => 'US'],
['id' => 2, 'name' => 'Canada', 'code' => 'CA'],
['id' => 3, 'name' => 'United Kingdom', 'code' => 'GB'],
['id' => 4, 'name' => 'Jamaica', 'code' => 'JM'],
['id' => 5, 'name' => 'Greece', 'code' => 'GR'],
];
}

You can then query this model as you would any other Eloquent model:

$northAmerica = Country::whereIn('code', ['US', 'JM'])->get();

Loading Data from Files

As your dataset grows, Truffle supports moving those records to CSV, JSON, or XML files. You can specify the file path using the $truffleFile property:

class Country extends Model
{
use Truffle;
 
protected $truffleFile = __DIR__ . '/data/countries.csv';
}

This allows you to keep data separate from your model logic while keeping it version-controlled.

Explicit Schema Definition

While Truffle can often infer column types, you can explicitly define the schema using the DataType enum to ensure database integrity and correct casting for your reference data:

use Waad\Truffle\Enums\DataType;
 
class Country extends Model
{
use Truffle;
 
protected $schema = [
'id' => DataType::Id,
'name' => DataType::String,
'code' => DataType::String,
'active' => DataType::Boolean,
];
}

Performance and Caching

Truffle includes caching to help with performance across requests. By enabling $truffleCache, the package caches the generated SQLite table structure and data:

protected $truffleCache = true;
protected $truffleCacheTtl = 3600; // 1 hour

Additionally, if you want to persist the "in-memory" database to a specific file instead of using memory, you can define a $truffleSqliteFile.

Installation

You can install the package via Composer:

composer require waad/truffle

The package works out of the box once the trait is added to your models. It requires PHP 7.4 or higher and supports Laravel 5.5 and above.

You can learn more about this package, get full installation instructions, and view the source code on GitHub.

Yannick Lyn Fatt photo

Staff Writer at Laravel News and Full stack web developer.

Cube

Laravel Newsletter

Join 40k+ other developers and never miss out on new tips, tutorials, and more.

image
SerpApi

The Web Search API for Your LLM and AI Applications

Visit SerpApi
PhpStorm logo

PhpStorm

The go-to PHP IDE with extensive out-of-the-box support for Laravel and its ecosystem.

PhpStorm
No Compromises logo

No Compromises

Joel and Aaron, the two seasoned devs from the No Compromises podcast, are now available to hire for your Laravel project. ⬧ Flat rate of $9500/mo. ⬧ No lengthy sales process. ⬧ No contracts. ⬧ 100% money back guarantee.

No Compromises
Shift logo

Shift

Running an old Laravel version? Instant, automated Laravel upgrades and code modernization to keep your applications fresh.

Shift
SaaSykit: Laravel SaaS Starter Kit logo

SaaSykit: Laravel SaaS Starter Kit

SaaSykit is a Multi-tenant Laravel SaaS Starter Kit that comes with all features required to run a modern SaaS. Payments, Beautiful Checkout, Admin Panel, User dashboard, Auth, Ready Components, Stats, Blog, Docs and more.

SaaSykit: Laravel SaaS Starter Kit
Acquaint Softtech logo

Acquaint Softtech

Acquaint Softtech offers AI-ready Laravel developers who onboard in 48 hours at $3000/Month with no lengthy sales process and a 100 percent money-back guarantee.

Acquaint Softtech
Lucky Media logo

Lucky Media

Get Lucky Now - the ideal choice for Laravel Development, with over a decade of experience!

Lucky Media
Harpoon: Next generation time tracking and invoicing logo

Harpoon: Next generation time tracking and invoicing

The next generation time-tracking and billing software that helps your agency plan and forecast a profitable future.

Harpoon: Next generation time tracking and invoicing
Laravel Cloud logo

Laravel Cloud

Easily create and manage your servers and deploy your Laravel applications in seconds.

Laravel Cloud
Kirschbaum logo

Kirschbaum

Providing innovation and stability to ensure your web application succeeds.

Kirschbaum
Tinkerwell logo

Tinkerwell

The must-have code runner for Laravel developers. Tinker with AI, autocompletion and instant feedback on local and production environments.

Tinkerwell

The latest

View all →
Detect and Resolve Laravel Schema Drift with MigrAlign image

Detect and Resolve Laravel Schema Drift with MigrAlign

Read article
Laravel Cloud Adds Scale-to-Zero and Spending Limits image

Laravel Cloud Adds Scale-to-Zero and Spending Limits

Read article
Shift + AI = Fully Automated Laravel Upgrades image

Shift + AI = Fully Automated Laravel Upgrades

Read article
Laracon AU 2026 Announces Full Speaker Lineup, Schedule, and Workshops image

Laracon AU 2026 Announces Full Speaker Lineup, Schedule, and Workshops

Read article
Parsel: Parse PDFs, Office Documents, and Images in PHP image

Parsel: Parse PDFs, Office Documents, and Images in PHP

Read article
Typed Objects for Eloquent with Expressive image

Typed Objects for Eloquent with Expressive

Read article