├── CHANGELOG.md ├── LICENSE.md ├── README-CONTRIBUTON.md ├── README.md ├── composer.json ├── config └── flymodel.php ├── database ├── factories │ └── ModelFactory.php └── migrations │ └── create_flymodel_table.php ├── docker-compose.yml ├── phpunit-postgress.xml.dist ├── resources └── views │ └── .gitkeep └── src ├── Commands └── FlyModelCommand.php ├── Facades └── FlyModel.php ├── FlyModel.php └── FlyModelServiceProvider.php /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | 3 | All notable changes to `FlyModel` will be documented in this file. 4 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) AuroraWebSoftware 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /README-CONTRIBUTON.md: -------------------------------------------------------------------------------- 1 | vendor/bin/pest 2 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | # FlyModel: Dynamic Laravel Models on the Fly 3 | 4 | FlyModel is a Laravel package that empowers you to create and manage models dynamically, **on the fly !**. 5 | 6 | It allows to define flexible and customizable models and model fields **without needing to change** the database schema. 7 | 8 | This package streamlines your workflow by eliminating the need to define models explicitly in your codebase. Instead, you can generate and interact with models as needed, based on a unique "deck" identifier. 9 | 10 | The package uses **FlexyField** Package as Dynamic Field Engine. 11 | 12 | For More Info Visit: https://github.com/AuroraWebSoftware/FlexyField 13 | 14 | ## 🚀 Features 15 | 16 | - **Dynamic Model Creation**: Instantiate models with a specified "deck" identifier without pre-defining them. 17 | - **Dynamic Fields** : Define flexible and customizable fields on models **without needing to change** the database schema. 18 | - **Automatic Scoping**: Models are automatically scoped according to the deck, ensuring data isolation. 19 | - **Seamless Integration**: Works effortlessly with Laravel’s Eloquent ORM. 20 | 21 | ## 📦 Installation 22 | 23 | To get started with FlyModel, follow these steps: 24 | 25 | ### Install the Package 26 | 27 | Add FlyModel to your project using Composer: 28 | 29 | ```bash 30 | composer require aurorawebsoftware/flymodel 31 | ``` 32 | 33 | ### Run the Migration 34 | 35 | Create the necessary database table for storing fly models: 36 | 37 | ```bash 38 | php artisan migrate 39 | ``` 40 | 41 | 42 | ## 📘 Usage 43 | 44 | ### Creating and Using Fly Models 45 | 46 | With FlyModel, you can dynamically create models and perform various operations. Here’s how: 47 | 48 | Instantiate a Model with a Deck 49 | 50 | ```php 51 | $building = FlyModel::of('building'); 52 | ``` 53 | 54 | Save the Model if not saved or created before 55 | 56 | ```php 57 | $building->save(); 58 | ``` 59 | 60 | Perform Field Operations 61 | ```php 62 | $building->flexy->name = 'Headquarter Building' 63 | $building->flexy->address = 'Ali Pasha Ave. number 10'; 64 | $building->flexy->city = 'İstanbul'; 65 | $building->flexy->floor = 7; 66 | $building->flexy->area = 313; 67 | $building->flexy->active = true; 68 | 69 | $building->save(); 70 | ``` 71 | 72 | 73 | Perform Eloquent Operations 74 | 75 | ```php 76 | $buildings = FlyModel::of('building')->all(); 77 | 78 | $istanbulBuildings = FlyModel::of('building') 79 | ->where('flexy_city', 'İstanbul') 80 | ->get(); 81 | 82 | $largeBuildings = FlyModel::of('building') 83 | ->where('flexy_area', '>' 500) 84 | ->orderBy('flexy_area') 85 | ->get(); 86 | 87 | $highBuildingsInIstanbul = FlyModel::of('building') 88 | ->where('flexy_floor', '>' 10) 89 | ->where('flexy_city', 'İstanbul') 90 | ->get(); 91 | ``` 92 | 93 | 94 | ## 🧪 Testing 95 | 96 | FlyModel integrates with Laravel’s testing environment. Here’s an example of how to write tests for it: 97 | 98 | 99 | 100 | ## 💬 Contributing 101 | 102 | We welcome contributions to improve FlyModel! 103 | ## 104 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "aurorawebsoftware/flymodel", 3 | "description": "Laravel Models on the FLY !", 4 | "keywords": [ 5 | "AuroraWebSoftware", 6 | "laravel", 7 | "flymodel" 8 | ], 9 | "homepage": "https://github.com/aurorawebsoftware/flymodel", 10 | "license": "MIT", 11 | "authors": [ 12 | { 13 | "name": "Aurora Web Software Team", 14 | "email": "websoftwareteam@aurorabilisim.com", 15 | "role": "Developer" 16 | } 17 | ], 18 | "require": { 19 | "php": "^8.2", 20 | "aurorawebsoftware/flexyfield": "^1.6.3", 21 | "illuminate/contracts": "^10.0||^11.0", 22 | "spatie/laravel-package-tools": "^1.16" 23 | }, 24 | "require-dev": { 25 | "laradumps/laradumps-core": "^2.0", 26 | "larastan/larastan": "^2.9", 27 | "laravel/pint": "^1.14", 28 | "nunomaduro/collision": "^8.1.1||^7.10.0", 29 | "orchestra/testbench": "^9.0.0||^8.22.0", 30 | "pestphp/pest": "^2.34", 31 | "pestphp/pest-plugin-arch": "^2.7", 32 | "pestphp/pest-plugin-laravel": "^2.3", 33 | "phpstan/extension-installer": "^1.3", 34 | "phpstan/phpstan-deprecation-rules": "^1.1", 35 | "phpstan/phpstan-phpunit": "^1.3" 36 | }, 37 | "autoload": { 38 | "psr-4": { 39 | "AuroraWebSoftware\\FlyModel\\": "src/", 40 | "AuroraWebSoftware\\FlyModel\\Database\\Factories\\": "database/factories/" 41 | } 42 | }, 43 | "autoload-dev": { 44 | "psr-4": { 45 | "AuroraWebSoftware\\FlyModel\\Tests\\": "tests/", 46 | "Workbench\\App\\": "workbench/app/" 47 | } 48 | }, 49 | "scripts": { 50 | "post-autoload-dump": "@composer run prepare", 51 | "clear": "@php vendor/bin/testbench package:purge-flymodel --ansi", 52 | "prepare": "@php vendor/bin/testbench package:discover --ansi", 53 | "build": [ 54 | "@composer run prepare", 55 | "@php vendor/bin/testbench workbench:build --ansi" 56 | ], 57 | "start": [ 58 | "Composer\\Config::disableProcessTimeout", 59 | "@composer run build", 60 | "@php vendor/bin/testbench serve" 61 | ], 62 | "analyse": "vendor/bin/phpstan analyse", 63 | "test": "vendor/bin/pest", 64 | "test-coverage": "vendor/bin/pest --coverage", 65 | "format": "vendor/bin/pint" 66 | }, 67 | "config": { 68 | "sort-packages": true, 69 | "allow-plugins": { 70 | "pestphp/pest-plugin": true, 71 | "phpstan/extension-installer": true 72 | } 73 | }, 74 | "extra": { 75 | "laravel": { 76 | "providers": [ 77 | "AuroraWebSoftware\\FlyModel\\FlyModelServiceProvider" 78 | ], 79 | "aliases": { 80 | "FlyModel": "AuroraWebSoftware\\FlyModel\\Facades\\FlyModel" 81 | } 82 | } 83 | }, 84 | "minimum-stability": "dev", 85 | "prefer-stable": true 86 | } 87 | -------------------------------------------------------------------------------- /config/flymodel.php: -------------------------------------------------------------------------------- 1 | id(); 13 | $table->string('deck')->index(); 14 | $table->timestamps(); 15 | }); 16 | } 17 | 18 | public function down() 19 | { 20 | Schema::dropIfExists('fly_models'); 21 | } 22 | }; 23 | -------------------------------------------------------------------------------- /docker-compose.yml: -------------------------------------------------------------------------------- 1 | version: '3.7' 2 | 3 | services: 4 | mariadb: 5 | image: mariadb:10.8 6 | ports: 7 | - "33064:3306" 8 | volumes: 9 | - ~/apps/flymodel/mariadb:/var/lib/mysql 10 | environment: 11 | - MYSQL_ROOT_PASSWORD=flymodel 12 | - MYSQL_PASSWORD=flymodel 13 | - MYSQL_USER=flymodel 14 | - MYSQL_DATABASE=flymodel 15 | postgres: 16 | image: postgres:15 17 | ports: 18 | - "54324:5432" 19 | volumes: 20 | - ~/apps/flymodel/postgres:/var/lib/postgresql/data 21 | environment: 22 | - POSTGRES_USER=flymodel 23 | - POSTGRES_PASSWORD=flymodel 24 | - POSTGRES_DB=flymodel 25 | 26 | networks: 27 | default: 28 | driver: bridge 29 | ipam: 30 | config: 31 | - subnet: 172.16.9.0/24 32 | -------------------------------------------------------------------------------- /phpunit-postgress.xml.dist: -------------------------------------------------------------------------------- 1 | 2 | 18 | 19 | 20 | tests 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | ./src 42 | 43 | 44 | 45 | -------------------------------------------------------------------------------- /resources/views/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AuroraWebSoftware/FlyModel/6503dfc0978c32e3c5f58a9a150a4ecbef985455/resources/views/.gitkeep -------------------------------------------------------------------------------- /src/Commands/FlyModelCommand.php: -------------------------------------------------------------------------------- 1 | comment('All done'); 16 | 17 | return self::SUCCESS; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/Facades/FlyModel.php: -------------------------------------------------------------------------------- 1 | where(function ($query) { 24 | $query->where('deck', '=', self::$deck); 25 | }); 26 | }); 27 | } 28 | 29 | public static string $deck = ''; 30 | 31 | protected $table = 'fly_models'; 32 | 33 | public static function getModelType(): string 34 | { 35 | return 'AuroraWebSoftware\FlyModel\FlyModel@'.self::$deck; 36 | } 37 | }; 38 | 39 | $instance::$deck = $deck; 40 | $instance->deck = $deck; 41 | 42 | return $instance; 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /src/FlyModelServiceProvider.php: -------------------------------------------------------------------------------- 1 | loadMigrationsFrom(__DIR__.'/../database/migrations'); 14 | 15 | return parent::boot(); 16 | } 17 | 18 | public function configurePackage(Package $package): void 19 | { 20 | /* 21 | * This class is a Package Service Provider 22 | * 23 | * More info: https://github.com/spatie/laravel-package-tools 24 | */ 25 | $package 26 | ->name('flymodel'); 27 | //->hasConfigFile() 28 | //->hasViews() 29 | //->hasMigration('create_flymodel_table') 30 | //->hasCommand(FlyModelCommand::class) 31 | } 32 | } 33 | --------------------------------------------------------------------------------