Sushi – Eloquent's missing "array" driver
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".
🤯 I just did a crazy thing to an Eloquent model.
— Caleb Porzio (@calebporzio) January 23, 2020
Something I've wanted for so long.
Thanks to, you guessed it. Sqlite ❤️ pic.twitter.com/JuQIczoTxD
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 if you could use an API endpoint for an Eloquent model instead of a database table???... 🤔👀 pic.twitter.com/UPPtFGYurL
— Caleb Porzio (@calebporzio) February 6, 2020
What's even more crazy is, how easy it is to get it up & running.
- 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.
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. :)