In Laravel projects, we usually store data in databases, create tables, and run migrations. But not every bit of data needs that level...
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.
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.
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
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();
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');
}
}
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:
Cache::remember()
method.getRows()
pulls and transforms the data into the format Sushi requires.Sushi is perfect when you need a lightweight solution for static or semi-static datasets, like:
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.
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.š§āš»š
Subscribe to our "Article Digest". We'll send you a list of the new articles, every week, month or quarter - your choice.
What do you think about this?
Wondering what our community has been up to?