Sushi – Eloquent's missing "array" driver

TLDR; In this article, I am going to discuss about Sushi, not the meal but something that can actually fill your appetite :).

Posted by Mohamed Fawzan on 2020-Feb-07

This is a special post about something that actually blown my mind right away. Sushi is a package written by one of the cool guys in the laravel community, Caleb Porzio. You might also know him by his project Laravel Liveware and AlpineJS. I really want to write about Liveware but that's for some other time, I want to talk about on of his other projects, called "Sushi".

A while back Caleb tweeted a video of a demo, The package literally turned a PHP array to eloquent resource. It blew my mind away because I could literally see a lot of application for that right away, Imagine a list of countries or a static list of data, what if you want to consider them as Eloquent rows, with relationships and all. Sushi let's you do that by caching the PHP array to a sqlite database and server you just the way you want it... Hot.. Hot.. 😍

What's even more amazing is, You can even use a rest API endpoint as the data source. How cool is that?

What's even more crazy is, how easy it is to get it up & running.

  1. Install the package
composer require calebporzio/sushi

2. Use the Trait

class State extends Model
{
    use \Sushi\Sushi;

    protected $rows = [
        [
            'abbr' => 'NY',
            'name' => 'New York',
        ],
        [
            'abbr' => 'CA',
            'name' => 'California',
        ],
    ];
}

Now, you don't have to stop there. If you are using Laravel Nova, You can create a resource to map this Model.

New is shocked about Sushi

New is shocked about Sushi

php artisan nova:resource State

And add the fields.

public function fields(Request $request)
{
    return [
        ID::make()->sortable(),
        Text::make('Abbr'),
        Text::make('Name'),
    ];
}

Of course, you need to make the resource read-only. But Imagine the possibilities here. All those relationships can easily be attached to the Model now.

public function authorizedToAdd(NovaRequest $request, $model)
{
    return false;
}

public function authorizedToDelete(Request $request)
{
    return false;
}

public function authorizedToUpdate(Request $request)
{
    return false;
}

If you haven't tried it out yet, you should give it a try now. You might find a better use can that what I've explained above ( Which I've stolen from the documentation)

PS: Caleb is trying his best to deliver good stuff to the community, If you are a kind heart, you should consider sponsoring him on github so that he can more build cool things.

Thanks for reading guys, If you like the post, Tweet it & Share it. I'll see you in another post soon. :)