š£ Sushi ā Your Eloquent model driver for other data sources
In Laravel projects, we usually store data in databases, create tables, and run migrations. But not every bit of data needs that level of complexity. Sometimes, you have small, static datasetsālike a list of countries, settings, or configsāthat hardly ever change. Setting up a database table for everything can feel like overkill.
Thatās where Sushi comes in. Itās a lightweight package that lets you skip the database entirely for small, static data, even pulling from APIs or config arrays.
What is Laravel Sushi?
Sushi allows you to create Eloquent models from data sources other than a database. Instead of setting up a traditional database, you can provide data from an array, config, API, or a CSV file. What I mean is... it can be anything.
It is created by Caleb Porzio (the guy behind Livewire and AlpineJS), Sushi simplifies your data when a full database table is unnecessary.
How to Use Sushi?
The simplest way to use Sushi is by providing data as a hardcoded array. But you can also pull dynamic data from an API or load it from a config array for flexibility. Letās explore three examples:
Installation Let's do the installation first.
composer require calebporzio/sushi
Example 1: Hardcoded Data
Hereās a basic model using a hardcoded array:
<?php
namespace App\Models;
use Sushi\Sushi;
class Country extends Model
{
// Add the Sushi trait
use Sushi;
// Provide data as a hardcoded array
protected $rows = [
['id' => 1, 'abbr' => 'IN', 'label' => 'India'],
['id' => 2, 'abbr' => 'US', 'label' => 'United States'],
];
}
Now, you can query it just like a regular database model:
$india = Country::where('abbr', 'IN')->first();
Example 2: Data from a Config Array
You can also load the data from a config file. If you have data in some third-party config or you choose to keep the data outside the model, you can set up eloquent models from that data without going deeper into the code.
Letās say you have a config file like this in config/countries.php
:
<?php
return [
['id' => 1, 'abbr' => 'IN', 'label' => 'India'],
['id' => 2, 'abbr' => 'US', 'label' => 'United States'],
];
Now, you can use Sushi to load this config data into a model:
<?php
namespace App\Models;
use Sushi\Sushi;
class Country extends Model
{
use Sushi;
public function getRows()
{
// Load the data from the config file
return config('countries');
}
}
Example 3: Dynamic Data from an API with Caching
You can also implement Sushi where you pull data from an API. Hereās how you can fetch dynamic data from an API and cache it for an hour:
<?php
namespace App\Models;
use Sushi\Sushi;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Cache;
class Country extends Model
{
use Sushi;
public function getRows()
{
// Cache the API data for 1 hour
return Cache::remember('countries', 60, function () {
$response = Http::get('https://restcountries.com/v3.1/all');
$countries = $response->json();
// Map the API response to the format Sushi expects
return collect($countries)->map(function ($country) {
return [
'id' => $country['cca3'],
'abbr' => $country['cca2'],
'label' => $country['name']['common'],
];
})->toArray();
});
}
}
In this example:
- Weāre fetching data from the REST Countries API.
- The API response is cached for 1 hour using Laravelās
Cache::remember()
method. -
getRows()
pulls and transforms the data into the format Sushi requires.
Why Use Sushi?
Sushi is perfect when you need a lightweight solution for static or semi-static datasets, like:
- Configurations, lists of countries, or languages
- Predefined roles or permissions
- Any small dataset that doesnāt change often but still needs to be queried
You can skip the database entirely and enjoy the full power of Eloquent without the overhead of migrations and database setup.
One more cool thing: You donāt even need an id
column unless you want to. Sushi can auto-generate IDs for each row. But keep in mind, if the data changes and the cache is reset, these IDs might change too. So, for stability, itās best to define your own id
column for each item.
Note: Since Sushi uses SQLite to store the data, make sure the SQLite extension is enabled in your PHP configuration.
Conclusion
Sushi lets you skip the database setup for smaller datasets and helps you define that data directly in the model, saving time and keeping things lightweight. It is a great tool when dealing with static data, configurations, or even dynamic data from APIs.
Just a side note, our core product CRUD is also compatible with Sushi and you can use it to create a Table for the entries. Our Backpack DevTools also uses Sushi, which shows how practical this tool is. Give it a try - save time and keep things light.š§āš»š
driesvints liked this article
Other articles you might like
Laravel Custom Query Builders Over Scopes
Hello š Alright, let's talk about Query Scopes. They're awesome, they make queries much easier to r...
Access Laravel before and after running Pest tests
How to access the Laravel ecosystem by simulating the beforeAll and afterAll methods in a Pest test....
Laravel Under The Hood - A Little Bit of Macros
Hello š How often have you wished for a method that doesn't exist on collections or string helpers?...
The Laravel portal for problem solving, knowledge sharing and community building.
The community