### Install Sushi Package Source: https://github.com/calebporzio/sushi/blob/main/README.md Install the Sushi package using Composer. This is the first step to enable Eloquent models to work with array data. ```bash composer require calebporzio/sushi ``` -------------------------------- ### Customizing Sushi Model Table with afterMigrate Source: https://github.com/calebporzio/sushi/blob/main/README.md Implement the `afterMigrate` method to customize the table after it's created by Sushi. This example adds an index to the 'name' column. ```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'); } } ``` -------------------------------- ### Querying Sushi Model Data Source: https://github.com/calebporzio/sushi/blob/main/README.md Query data from a Sushi model just like a regular Eloquent model. This example shows how to retrieve a state's name by its abbreviation. ```php $stateName = State::whereAbbr('NY')->first()->name; ``` -------------------------------- ### Basic Model Setup with $rows Source: https://context7.com/calebporzio/sushi/llms.txt Define static data using the $rows property in your Eloquent model. This automatically creates an in-memory SQLite table for full Eloquent operations. ```php 'NY', 'name' => 'New York'], ['abbr' => 'CA', 'name' => 'California'], ['abbr' => 'TX', 'name' => 'Texas'], ['abbr' => 'FL', 'name' => 'Florida'], ]; } ``` ```php // Usage examples $states = State::all(); // Get all states $state = State::whereAbbr('NY')->first(); // Find by abbreviation $name = State::find(1)->name; // Find by ID $count = State::count(); // Count records $californiaName = State::where('abbr', 'CA')->value('name'); // Get single value ``` -------------------------------- ### Define Role Model with Sushi Source: https://github.com/calebporzio/sushi/blob/main/README.md Define a Role model using the Sushi trait and a $rows property. This serves as an example for creating fixture data for roles. ```php class Role extends Model { use \Sushi\Sushi; protected $rows = [ ['id' => 1, 'label' => 'admin'], ['id' => 2, 'label' => 'manager'], ['id' => 3, 'label' => 'user'], ]; } ``` -------------------------------- ### Customizing Sushi Model Schema Source: https://github.com/calebporzio/sushi/blob/main/README.md Customize the schema for a Sushi model using the protected $schema property. This example sets the 'price' column to 'float'. ```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', ]; } ``` -------------------------------- ### Using Exists Validation Rule with Sushi Model Source: https://github.com/calebporzio/sushi/blob/main/README.md Example of using Laravel's `exists` validation rule with a Sushi model. It requires the fully-qualified namespace to ensure correct model resolution. ```php $data = request()->validate([ 'state' => ['required', 'exists:App\Models\State,abbr'], ]); ``` -------------------------------- ### Associating and Accessing Sushi Model Relationships Source: https://github.com/calebporzio/sushi/blob/main/README.md Shows how to associate a User with a Role and access the relationship. This includes eager loading the relationship. ```php // Grab a User. $user = User::first(); // Grab a Role. $role = Role::whereLabel('admin')->first(); // Associate them. $user->role()->associate($role); // Access like normal. $user->role; // Eager load. $user->load('role'); User::with('role')->first(); ``` -------------------------------- ### Caching Dynamic Data with sushiShouldCache() Source: https://context7.com/calebporzio/sushi/llms.txt Enable caching for dynamic getRows() data by implementing sushiShouldCache(). Use sushiCacheReferencePath() to specify a file that, when changed, busts the cache. ```php first(); ``` -------------------------------- ### Customizing SQLite Tables with afterMigrate() Source: https://context7.com/calebporzio/sushi/llms.txt Implement the `afterMigrate()` method in Sushi models to perform custom SQLite table operations post-creation, such as adding indexes or default values. ```php 'LM-001', 'name' => 'Lawn Mower', 'category' => 'garden'], ['sku' => 'LB-002', 'name' => 'Leaf Blower', 'category' => 'garden'], ['sku' => 'RK-003', 'name' => 'Rake', 'category' => 'garden'], ['sku' => 'HM-004', 'name' => 'Hammer', 'category' => 'tools'], ]; protected function afterMigrate(Blueprint $table) { // Add indexes for frequently queried columns $table->index('sku'); $table->index('category'); // Add a column with default value $table->boolean('active')->default(true); } } // Queries benefit from indexes $product = SearchableProduct::where('sku', 'LM-001')->first(); $gardenProducts = SearchableProduct::where('category', 'garden')->get(); ``` -------------------------------- ### Eloquent Relationship with Sushi Model Source: https://github.com/calebporzio/sushi/blob/main/README.md Demonstrates setting up a standard Eloquent relationship (belongsTo) from a regular model to a Sushi model. Note that `whereHas` will not work with Sushi models due to separate databases. ```php class User extends Model { ... public function role() { return $this->belongsTo(Role::class); } } ``` -------------------------------- ### Eloquent Relationships with Sushi Models Source: https://context7.com/calebporzio/sushi/llms.txt Define and use Eloquent relationships between Sushi models and regular database-backed models. Ensure necessary imports and model definitions are in place. ```php 1, 'name' => 'admin', 'permissions' => 'all'], ['id' => 2, 'name' => 'manager', 'permissions' => 'read,write'], ['id' => 3, 'name' => 'user', 'permissions' => 'read'], ]; public function users() { return $this->hasMany(User::class); } } // Regular database model class User extends Model { protected $fillable = ['name', 'email', 'role_id']; public function role() { return $this->belongsTo(Role::class); } } // Usage examples $user = User::first(); $userRole = $user->role; // Access Sushi model via relationship $roleName = $user->role->name; // Get role name $adminRole = Role::whereName('admin')->first(); $user->role()->associate($adminRole); // Associate relationships $user->save(); // Eager loading works $usersWithRoles = User::with('role')->get(); // Note: whereHas() does NOT work across Sushi/DB boundaries // $admins = User::whereHas('role', fn($q) => $q->where('name', 'admin'))->get(); // Won't work ``` -------------------------------- ### Configure Cache Reference Path for External Data Sources Source: https://github.com/calebporzio/sushi/blob/main/README.md Use `sushiCacheReferencePath()` to specify an external file (e.g., a CSV) that Sushi should reference for cache invalidation. This is useful when your `getRows()` method reads from external 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'; } } ``` -------------------------------- ### Configure String-Based Primary Key in Sushi Model Source: https://github.com/calebporzio/sushi/blob/main/README.md For models with string-based primary keys, set `$incrementing` to `false` and `$keyType` to `'string'`. This tells Sushi to use string values for the primary key. ```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'], ]; } ``` -------------------------------- ### Configure Insert Chunk Size for Large Datasets Source: https://context7.com/calebporzio/sushi/llms.txt Set the `$sushiInsertChunkSize` property to a lower value when dealing with large datasets that have many columns to prevent exceeding SQLite variable limits. ```php '10001', 'city' => 'New York', 'state' => 'NY', 'lat' => 40.7484, 'lng' => -73.9967], ['code' => '90210', 'city' => 'Beverly Hills', 'state' => 'CA', 'lat' => 34.0901, 'lng' => -118.4065], // ... thousands more rows ]; protected $schema = [ 'lat' => 'float', 'lng' => 'float', ]; } // Works with large datasets $nyZips = ZipCode::where('state', 'NY')->get(); $nearbyZips = ZipCode::whereBetween('lat', [34.0, 34.5])->get(); ``` -------------------------------- ### Dynamic Row Generation with getRows() Source: https://github.com/calebporzio/sushi/blob/main/README.md Implement the `getRows()` method to dynamically determine the model's rows at runtime, instead of using the protected $rows property. This allows for generating rows from external sources. ```php class Role extends Model { use \Sushi\Sushi; public function getRows() { return [ ['id' => 1, 'label' => 'admin'], ['id' => 2, 'label' => 'manager'], ['id' => 3, 'label' => 'user'], ]; } } ``` -------------------------------- ### String-based Primary Keys in Sushi Models Source: https://context7.com/calebporzio/sushi/llms.txt Configure Sushi models to use string-based primary keys by setting `$incrementing` to `false` and `$keyType` to 'string', and specifying `$primaryKey`. ```php 'users.view', 'name' => 'View Users', 'group' => 'users'], ['slug' => 'users.create', 'name' => 'Create Users', 'group' => 'users'], ['slug' => 'users.edit', 'name' => 'Edit Users', 'group' => 'users'], ['slug' => 'posts.view', 'name' => 'View Posts', 'group' => 'posts'], ['slug' => 'posts.create', 'name' => 'Create Posts', 'group' => 'posts'], ]; } // Find by string key $permission = Permission::find('users.view'); $permissionName = $permission->name; // "View Users" // Query normally $userPermissions = Permission::where('group', 'users')->get(); ``` -------------------------------- ### Define Schema for Empty Datasets in Sushi Model Source: https://github.com/calebporzio/sushi/blob/main/README.md When using `getRows()` that might return an empty array, define an optional `$schema` property to specify the table structure. This prevents errors when Sushi cannot infer the schema from the first row. ```php class Currency extends Model { use \Sushi\Sushi; protected $schema = [ 'id' => 'integer', 'name' => 'string', 'symbol' => 'string', 'precision' => 'float' ]; public function getRows() { return []; } } ``` -------------------------------- ### Dynamic Rows with getRows() Source: https://context7.com/calebporzio/sushi/llms.txt Implement the getRows() method to generate data dynamically at runtime. This is useful for data fetched from APIs, files, or other external sources. ```php 'USD', 'rate' => 1.00, 'symbol' => '$'], ['currency' => 'EUR', 'rate' => 0.85, 'symbol' => '€'], ['currency' => 'GBP', 'rate' => 0.73, 'symbol' => '£'], ['currency' => 'JPY', 'rate' => 110.25, 'symbol' => '¥'], ]; } } ``` ```php // Usage $euroRate = ExchangeRate::whereCurrency('EUR')->first()->rate; $allRates = ExchangeRate::orderBy('rate', 'desc')->get(); ``` -------------------------------- ### Adjust Insert Chunk Size for Large Datasets Source: https://github.com/calebporzio/sushi/blob/main/README.md If you encounter 'too many SQL variables' errors, adjust `sushiInsertChunkSize` in your model to a smaller value. The default is 100. ```php public $sushiInsertChunkSize = 50; ``` -------------------------------- ### Enable Caching for getRows() in Sushi Model Source: https://github.com/calebporzio/sushi/blob/main/README.md Use `sushiShouldCache()` to force Sushi to cache datasets returned by your custom `getRows()` method. This ensures data is cached between requests. ```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; } } ``` -------------------------------- ### Define State Model with Sushi Source: https://github.com/calebporzio/sushi/blob/main/README.md Define a model with the Sushi trait and a protected $rows property to represent array data. This allows the model to behave like an Eloquent model backed by a database. ```php class State extends Model { use \Sushi\Sushi; protected $rows = [ [ 'abbr' => 'NY', 'name' => 'New York', ], [ 'abbr' => 'CA', 'name' => 'California', ], ]; } ``` -------------------------------- ### Custom Schema Definition with $schema Source: https://context7.com/calebporzio/sushi/llms.txt Define explicit column types using the $schema property for custom type handling or when dealing with empty datasets. This overrides auto-detection. ```php 'Lawn Mower', 'price' => '226.99', 'in_stock' => true], ['name' => 'Leaf Blower', 'price' => '134.99', 'in_stock' => false], ['name' => 'Rake', 'price' => '9.99', 'in_stock' => true], ]; // Override auto-detected types protected $schema = [ 'price' => 'float', 'in_stock' => 'boolean', ]; } ``` ```php // For potentially empty datasets, schema is required class Currency extends Model { use Sushi; protected $schema = [ 'id' => 'integer', 'code' => 'string', 'name' => 'string', 'rate' => 'float', ]; public function getRows() { return []; // Empty dataset works with schema defined } } ``` ```php // Usage $expensiveProducts = Product::where('price', '>', 100)->get(); $availableProducts = Product::where('in_stock', true)->get(); ``` -------------------------------- ### Validation with Exists Rule for Sushi Models Source: https://context7.com/calebporzio/sushi/llms.txt Utilize Laravel's `exists` validation rule with Sushi models by providing the fully-qualified class name. This ensures data integrity by checking against the Sushi model's data. ```php validate([ 'name' => ['required', 'string', 'max:255'], 'email' => ['required', 'email', 'unique:users'], // Use fully-qualified class name for Sushi models 'state' => ['required', 'exists:App\Models\State,abbr'], 'role_id' => ['required', 'exists:App\Models\Role,id'], ]); // Validation passes only if state abbreviation exists in State model // and role_id exists in Role model return User::create($validated); } } // Test validation $request = new Request([ 'name' => 'John Doe', 'email' => 'john@example.com', 'state' => 'NY', // Valid - exists in State model 'role_id' => 1, // Valid - exists in Role model ]); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.