Get "PHP 8 in a Nuthshell" (Now comes with PHP 8.3)
Amit Merchant

Amit Merchant

A blog on PHP, JavaScript, and more

Using arrays as a lightweight database in Laravel

There are times when you don’t want to use a full-fledged database like MySQL or PostgreSQL for your application. For instance, if you’re building a small application that relies heavily on reading small subset of data, you might not want to setup a database table for something like that.

In such cases, you can use arrays as a lightweight database in your Laravel application. Luckily, there’s a package called Sushi that allows you to do just that.

Essentially, Sushi allows you to use arrays as a database in your Laravel application. Behind the scenes, Sushi uses SQLite to store the data in the array. So, you don’t have to worry about the data being lost when the server restarts.

To get started, you can install the Sushi package via Composer like so.

composer require calebporzio/sushi

Once installed, you can start using it in your models.

For instance, let’s say you have a Menu model that has a name and price attribute. You can use Sushi to store the data in an array like so.

class Menu extends Model
{
    use \Sushi\Sushi;

    protected $rows = [
        [
            'name' => 'Chicken Burger',
            'price' => 10,
        ],
        [
            'name' => 'Veg Burger',
            'price' => 8,
        ],
        [
            'name' => 'French Fries',
            'price' => 5,
        ],
    ];
}

As you can tell, to use Sushi, you need to use the Sushi trait in your model and define a $rows property which is an array of data you want to store in your model.

Note: Since Sushi uses SQLite to store the data, you need to make sure that the SQLite extension is enabled in your PHP configuration. Also, make sure you remove any existing traits that are being used such as HasFactory etc. from your model.

Failing to do so will result in weird errors.

Now, you can use this model as you would use any other model in your application. For instance, you can fetch all the rows from the menus table like so.

$menus = Menu::all();

You can also use the where method to filter the results like so.

$menus = Menu::where('price', '>', 5)->get()->toArray();

/*
[
    [
        'name' => 'Chicken Burger',
        'price' => 10,
    ],
    [
        'name' => 'Veg Burger',
        'price' => 8,
    ],
]
*/

Apart from using the $rows property, you can also use the getRows method to fetch the data from the database. This method is useful when you want to fetch the data from an external source such as an API.

class Menu extends Model
{
    use \Sushi\Sushi;

    public function getRows()
    {
        return Http::get('https://example.com/api/menus')->json();
    }
}

When using the getRows method, Sushi doesn’t cache the data. So, it will fetch the data from the external source every time you use the model.

To cache the data, You can force Sushi to cache your dataset with the following sushiShouldCache() method.

class Menu extends Model
{
    use \Sushi\Sushi;

    public function getRows()
    {
        return Http::get('https://example.com/api/menus')->json();
    }

    public function sushiShouldCache()
    {
        return true;
    }
}

Learn more about Sushi on its official website.

Learn the fundamentals of PHP 8 (including 8.1, 8.2, and 8.3), the latest version of PHP, and how to use it today with my new book PHP 8 in a Nutshell. It's a no-fluff and easy-to-digest guide to the latest features and nitty-gritty details of PHP 8. So, if you're looking for a quick and easy way to PHP 8, this is the book for you.

Like this article? Consider leaving a

Tip

👋 Hi there! I'm Amit. I write articles about all things web development. You can become a sponsor on my blog to help me continue my writing journey and get your brand in front of thousands of eyes.

Comments?