### Install Sushi Package Source: https://github.com/calebporzio/sushi/blob/main/README.md Installs the Sushi package using Composer. This is the first step to using Eloquent models with array data. ```bash composer require calebporzio/sushi ``` -------------------------------- ### Customizing Table with afterMigrate Method Source: https://github.com/calebporzio/sushi/blob/main/README.md Shows how to customize the generated table after migration using the `afterMigrate` method, for example, to add indexes. ```php class Products extends Model { use \Sushi\Sushi; protected $rows = [ ['name' => 'Lawn Mower', 'price' => '226.99'], ['name' => 'Leaf Blower', 'price' => '134.99'], ['name' => 'Rake', 'price' => '9.99'], ]; protected function afterMigrate(Blueprint $table) { $table->index('name'); } } ``` -------------------------------- ### Using getRows() Method for Dynamic Data Source: https://github.com/calebporzio/sushi/blob/main/README.md Illustrates how to opt out of the `$rows` property and implement a `getRows()` method to dynamically determine the model's data at runtime. ```php class Role extends Model { use \Sushi\Sushi; public function getRows() { return [ ['id' => 1, 'label' => 'admin'], ['id' => 2, 'label' => 'manager'], ['id' => 3, 'label' => 'user'], ]; } } ``` -------------------------------- ### Enable Caching for Custom getRows() Source: https://github.com/calebporzio/sushi/blob/main/README.md Demonstrates how to enable caching for datasets returned by a custom `getRows()` method in a Sushi model by implementing `sushiShouldCache()` and returning `true`. ```php class Role extends Model { use \Sushi\Sushi; public function getRows() { return [ ['id' => 1, 'label' => 'admin'], ['id' => 2, 'label' => 'manager'], ['id' => 3, 'label' => 'user'], ]; } protected function sushiShouldCache() { return true; } } ``` -------------------------------- ### Sushi Model Relationships Source: https://github.com/calebporzio/sushi/blob/main/README.md Shows how to define relationships between a Sushi model and a standard Eloquent model. It illustrates associating and accessing relationships, with a note on the limitation of `whereHas`. ```php class Role extends Model { use \Sushi\Sushi; protected $rows = [ ['id' => 1, 'label' => 'admin'], ['id' => 2, 'label' => 'manager'], ['id' => 3, 'label' => 'user'], ]; } class User extends Model { ... public function role() { return $this->belongsTo(Role::class); } } // Usage: $user = User::first(); $role = Role::whereLabel('admin')->first(); $user->role()->associate($role); $user->role; $user->load('role'); User::with('role')->first(); ``` -------------------------------- ### Cache Reference Path for External Files Source: https://github.com/calebporzio/sushi/blob/main/README.md Shows how to specify an external file path using `sushiCacheReferencePath()` to control cache invalidation when using custom data sources like CSV files. ```php class Role extends Model { use \Sushi\Sushi; public function getRows() { return CSV::fromFile(__DIR__.'/roles.csv')->toArray(); } protected function sushiShouldCache() { return true; } protected function sushiCacheReferencePath() { return __DIR__.'/roles.csv'; } } ``` -------------------------------- ### Define Schema for Empty Datasets Source: https://github.com/calebporzio/sushi/blob/main/README.md Illustrates how to define an explicit schema using the `$schema` property to handle cases where `getRows()` returns an empty array, preventing errors. ```php class Currency extends Model { use \Sushi\Sushi; protected $schema = [ 'id' => 'integer', 'name' => 'string', 'symbol' => 'string', 'precision' => 'float' ]; public function getRows() { return []; } } ``` -------------------------------- ### String-Based Primary Keys Configuration Source: https://github.com/calebporzio/sushi/blob/main/README.md Explains the necessary model properties (`$incrementing` and `$keyType`) required when using string-based primary keys in Sushi models. ```php class Role extends Model { use \Sushi\Sushi; public $incrementing = false; protected $keyType = 'string'; protected $rows = [ ['id' => 'admin', 'label' => 'Admin'], ['id' => 'manager', 'label' => 'Manager'], ['id' => 'user', 'label' => 'User'], ]; } ``` -------------------------------- ### Using Database Validation Rules with Sushi Source: https://github.com/calebporzio/sushi/blob/main/README.md Explains how to use Laravel's `exists:table,column` validation rule with a Sushi model. It highlights the need to use the fully-qualified model namespace. ```php $data = request()->validate([ 'state' => ['required', 'exists:App\Models\State,abbr'], ]); ``` -------------------------------- ### Basic Model with Sushi Trait Source: https://github.com/calebporzio/sushi/blob/main/README.md Demonstrates how to add the Sushi trait to an Eloquent model and define its data using the protected $rows property. This allows the model to be used like a database table. ```php class State extends Model { use \Sushi\Sushi; protected $rows = [ [ 'abbr' => 'NY', 'name' => 'New York', ], [ 'abbr' => 'CA', 'name' => 'California', ], ]; } // Usage: $stateName = State::whereAbbr('NY')->first()->name; ``` -------------------------------- ### Customizing Schema with $schema Property Source: https://github.com/calebporzio/sushi/blob/main/README.md Demonstrates how to define a custom schema for a Sushi model using the protected $schema property to specify data types for columns. ```php class Products extends Model { use \Sushi\Sushi; protected $rows = [ ['name' => 'Lawn Mower', 'price' => '226.99'], ['name' => 'Leaf Blower', 'price' => '134.99'], ['name' => 'Rake', 'price' => '9.99'], ]; protected $schema = [ 'price' => 'float', ]; } ``` -------------------------------- ### Adjusting Insert Chunk Size Source: https://github.com/calebporzio/sushi/blob/main/README.md Provides a solution for the 'too many SQL variables' error by configuring the `$sushiInsertChunkSize` property to a smaller value. ```php public $sushiInsertChunkSize = 50; ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.