Laravel Fixtures using Sushi

Manish Sharma
3 min readAug 4, 2023

--

Laravel Fixtures using Sushi
Laravel Fixtures using Sushi

If you are from Django world, fixtures are not new to you. Fixtures are used to populate the database with initial data. This approach also helps us during testing, when we want to use some preopulated data.

Laravel has the concept of seeders. We can populate a database with data using seed classes. We can create a Factory to perform seeding using Faker data. During deployment CI/CD scripts are executed to run these seeders.

But now we have an alternative approach : Sushi.

What is Sushi ?

As per docs: Sushi is the Eloquent missing “array” driver that allows us to use Eloquent without using Database.

What does it mean ?

Putting it simply, you just have to create an array variable named $rows or a method getRows() , used by Sushi to “populate” table with those records.

Here is using $rows variable (It’s an array of array)

//sunsigns
protected $rows = [
["id" => 1, "name" => "Aries", "symbol" => "♈️", "daterange" => "March 21 - April 19"],
["id" => 2, "name" => "Taurus", "symbol" => "♉️", "daterange" => "April 20 - May 20"],
["id" => 3, "name" => "Gemini", "symbol" => "♊️", "daterange" => "May 21 - June 20"],
["id" => 4, "name" => "Cancer", "symbol" => "♋️", "daterange" => "June 21 - July 22"],
["id" => 5, "name" => "Leo", "symbol" => "♌️", "daterange" => "July 23 - August 22"],
["id" => 6, "name" => "Virgo", "symbol" => "♍️", "daterange" => "August 23 - September 22"],
["id" => 7, "name" => "Libra", "symbol" => "♎️", "daterange" => "September 23 - October 22"],
["id" => 8, "name" => "Scorpio", "symbol" => "♏️", "daterange" => "October 23 - November 21"],
["id" => 9, "name" => "Sagittarius", "symbol" => "♐️", "daterange" => "November 22 - December 21"],
["id" => 10, "name" => "Capricorn", "symbol" => "♑️", "daterange" => "December 22 - January 19"],
["id" => 11, "name" => "Aquarius", "symbol" => "♒️", "daterange" => "January 20 - February 18"],
["id" => 12, "name" => "Pisces", "symbol" => "♓️", "daterange" => "February 19 - March 20"]
];

This is how we can load records from a JSON file:

 public function getRows()
{
//true -> get records as array instead of objects
return json_decode(\File::get('fixtures/continents.json'),true);
}

What if json file is modified ?

We can define these two methods to help Sushi determine if cache needs to be revalidated:

//Allow result to be cached
protected function sushiShouldCache()
{
return true;
}
//rebuild cache whenever this file is updated
protected function sushiCacheReferencePath()
{
return public_path().'/fixtures/continents.json';
}

We can use eloquent queries with supercharged “Sushi Models” very much like Simple Models:

//get all records 
$signs=AstroSign::all();
//get single record
$europe=Continent::where([['code','=','EU']])->first();

Loading JSON DATA

Use of Sushi in Laravel to Load Fixtures from json file
We can Sushi in Laravel to Load Fixtures from json file

In our Sample App we have populated “Continent” model with “public/fixtures/continents.json” json file that has following entries:

[
{
"id": 1,
"code": "AF",
"name": "Africa"
},
{
"id": 2,
"code": "AN",
"name": "Antarctica"
}
]

The complete code is:

That’s all. To know more about Sushi refer to this documentation.

In my opinion sushi is a good choice to load initial data when we know in advance that there is a finite predefined set of records that will not be modified too often.

You may download sample code from this repository. Run App and type http://127.0.0.1:8000/ in browser to see the output.

Happy Coding.

--

--