zablockibros / laravel-schemaless
Make models with no schema
Requires
- php: ^7.1.3
- doctrine/dbal: ^2.9
- illuminate/config: ~5.7.0
- illuminate/console: ~5.7.0
- illuminate/database: ~5.7.0
- illuminate/support: ~5.7.0
Requires (Dev)
- fzaninotto/faker: ^1.4
- mockery/mockery: ^1.0
- phpunit/phpunit: ~7.0
This package is auto-updated.
Last update: 2024-04-05 18:38:14 UTC
README
Create and prototype with models without needing to create migrations and alterations to a database schema.
Why?
When quickly prototyping an API or web app, you will be constantly adding and altering table columns and relationships to your model. With Schemaless, you can simply create models and extend the ZablockiBros\Models\Item
model and define $extraAttributes
. Create records, set attributes, fill records all the ways you normally would in Laravel.
Installation
Requirements: This package requires PHP 7.1.3 or higher and Laravel 5.7
-
Install the package via Composer:
$ composer require zablockibros/laravel-schemaless
The package will automatically register its service provider.
-
Publish the package:
php artisan vendor:publish --provider="ZablockiBros\Schemaless\SchemalessServiceProvider"
-
Run migrations:
php artisan migrate
Configuring Your Models
You can use schemaless attributes and relationships using your own tables or the migration-provided items
tables and the base Item
model class.
Using Item Base Model
-
Create your model:
php artisan make:model Models/YourModel
-
Extend the
ZablockiBros\Models\Item
class:class YourModel extends \ZablockiBros\Models\Item
-
Define your schemaless attributes:
<?php namespace App\Models; use ZablockiBros\Schemaless\Models\Item; class YourModel extends Item { /** * @var array */ protected $extraAttributes = [ 'name' => null, // attribute 'name' with default null 'options' => null, 'qty' => 0, ]; /** * @var array */ protected $casts = [ 'options' => 'array', ]; }
Using Your Own Table
You are able to use your own existing tables and models with schemaless attributes, relationships, and tables.
Schemaless provides three core traits:
HasSchemalessAttributes
,HasSchemalessRelationships
, andHasSchemalessTable
.
To setup your model:
- Create your table and model (only create a new table if you are NOT using
HasSchemalessTable
). - Add the desired traits to your model.
- Configure your
$extraAttributes
and relations as described above.
Notes:
- If you are using
HasSchemalessAttributes
, make sure your table has ajson
type column namedcolumns
. If you want to name it something else, override the column name with the following method on your model:/** * @return string */ protected function getExtraAttributesColumnName() { return 'my_columns'; }
- If you are using
HasSchemalessRelations
, make sure your table has amorphs
column calleditemable
(or your own name). If you use a different morph name, override the following on your model:/** * @return string */ public function getItemMorphColumnName() { return 'itemable'; }
- If you are using the
HasSchemalessTable
trait, you must use theitems
table for your schemaless model.
Accessing Attributes
You can access attributes the same way you normally would with an Eloquent model:
$yourModel->name; // 'test'
Saving Attributes
Save attributes the same way as well:
YourModel::create([ 'name' => 'test', ]); YourModel::firstOrNew([ 'name' => 'test', ]); $yourModel->fill([ 'name' => 'test', ]); YourModel::where(...) ->update([ 'name' => 'test', ]); $yourModel->name = 'test'; $yourModel->save();
Relationships
In progress
Note: Querying Attributes
Schemaless attributes need to be queried at the json
column in the models table (items
in the case where the model extends Item
).
YourModel::where('columns->name', 'test');
Copyright and License
Copyright (c) 2019 Justin Zablocki