awssat / laravel-kabsa
Laravel Array database
Requires
- php: ^7.3|^8.0
- illuminate/support: ~5.5.0 || ~5.6.0 || ~5.7.0 || ~5.8.0 || ^6.0 || ^7.0 || ^8.0 || ^9.0
Requires (Dev)
- doctrine/dbal: ^2.6|^3.0
- guzzlehttp/guzzle: ^6.5|^7.0
- illuminate/support: ~5.5.0 || ~5.6.0 || ~5.7.0 || ~5.8.0 || ^6.0 || ^7.0 || ^8.0
- mockery/mockery: ^1.4
- orchestra/testbench: ^3.5 || ^3.6 || ^3.7 || ^3.8 || ^4.0 || ^5.0 || ^6.0
- phpunit/phpunit: ^9.3
README
Laravel Kabsa is a simple array trait for your Eloquent model just like https://github.com/calebporzio/sushi without SQLite
Install
To get started with Laravel Kabsa, use Composer to add the package to your project's dependencies:
composer require awssat/laravel-kabsa
Examples
Code examples available see: examples
Use
- Add the
Kabsa
trait to a model. - Add a
$rows
property to the model.
class State extends Model { use \Awssat\Kabsa\Traits\Kabsa; protected $rows = [ [ 'abbr' => 'NY', 'name' => 'New York', ], [ 'abbr' => 'CA', 'name' => 'California', ], ]; }
or
class State extends Model { use \Awssat\Kabsa\Traits\Kabsa; public function getRows() { return [ [ 'abbr' => 'NY', 'name' => 'New York', ], [ 'abbr' => 'CA', 'name' => 'California', ], ]; } }
Now, you can use this model anywhere you like, and it will behave as if you created a table with the rows you provided.
$stateName = State::where('Abbr', 'NY')->first()->name; // or $stateName = State::firstWhere('Abbr', 'NY')->name;
Relationships
class Role extends Model { use \Awssat\Kabsa\Traits\Kabsa; protected $rows = [ ['label' => 'admin'], ['label' => 'manager'], ['label' => 'user'], ]; }
You can add a relationship to another standard model with help of new trait called KabsaRelationships
right now I have just added two relationships hope we add more
class User extends Model { use \Awssat\Kabsa\Traits\KabsaRelationships; public function role() { return $this->belongsToKabsaRow(Role::class, 'role_label', 'label'); } }
the users
table should have a role_label
column, then:
// Grab a User. $user = User::first(); // Grab a Role. $role = Role::where('label', 'admin')->first(); // Associate them. $user->role()->associate($role); // Access like normal. $user->role;
Eager loading doesn't work because it's a collection you don't need eager load you can call the relation ->relation
directly. If you need it to be appended to the collection array you can create an attribute and add it to $appends.
License
The MIT License (MIT). Please see License File for more information.