albaroody/staging
Composer 安装命令:
composer require albaroody/staging
包简介
Allow any Laravel model to be saved as a draft (staged) into a separate system without affecting real database tables.
关键字:
README 文档
README
Laravel Staging allows you to stage (draft) Eloquent models and their nested relationships into a clean, separate system before committing them permanently to your main database.
- Stage parent models like
Patient,Post,Order, etc. - Stage related models like
Sales,Items,Comments, etc. - Hydrate full Eloquent models from staged data (not just arrays)
- Promote staged data to production tables cleanly
- Keep your main database structure untouched — no intrusive columns added!
Perfect for multi-step forms, draft publishing systems, and modular deferred saving workflows.
Installation
You can install the package via Composer:
composer require albaroody/staging
You can publish and run the staging migration with
php artisan vendor:publish --tag="staging-migrations"
php artisan migrate
You can publish the config file with:
php artisan vendor:publish --tag="staging-config"
Usage
- Add the Stagable trait to your model:
use Albaroody\\Staging\\Traits\\Stagable; class Patient extends Model { use Stagable; }
- Stage a model:
// Option 1: Stage an existing model instance $patient = new Patient([ 'name' => 'John Doe', ]); $stagingId = $patient->stage(); // Option 2: Stage a new model using static method $stagingId = Patient::stageNew([ 'name' => 'John Doe', ]);
- Load a staged model:
$stagedPatient = Patient::findStaged($stagingId); // Now you can use it like a normal model echo $stagedPatient->name;
- Promote a staged model to the database:
$realPatient = $stagedPatient->promoteFromStaging(); // The staging entry is automatically deleted after promotion
HasMany Relationships
The library supports staging hasMany relationships, allowing you to create multiple child records before saving the parent.
Basic Usage
- Stage multiple children at once:
$stockLevels = [ ['quantity' => 100, 'location' => 'Warehouse A', '_sort_order' => 0], ['quantity' => 50, 'location' => 'Warehouse B', '_sort_order' => 1], ]; $stagingIds = StockLevel::stageMany($stockLevels);
- Link staged children to a parent:
// Stage the parent first $product = new Product(['name' => 'Widget', 'price' => 10.00]); $productStagingId = $product->stage(); // Link staged children to parent StockLevel::linkStagedToParent($stagingIds, $productStagingId, Product::class);
- Find staged children by parent:
$stagedChildren = StockLevel::findStagedChildren($productStagingId, Product::class); foreach ($stagedChildren as $child) { echo $child['data']['location']; // 'Warehouse A', 'Warehouse B' }
- Save staged children when parent is saved:
// Save the parent $realProduct = $product->promoteFromStaging(); // Save all staged children with the parent's ID $createdIds = StockLevel::saveStagedChildren( $realProduct->id, $productStagingId, Product::class, 'product_id' // foreign key column name );
Automatic Child Saving
You can configure models to automatically save staged children when the parent is created via HTTP request:
- Define hasMany relationships in your model:
use Albaroody\Staging\Traits\Stagable; class Product extends Model { use Stagable; protected static function getHasManyRelationships(): array { return [ 'stock_levels' => [ 'model' => StockLevel::class, 'foreign_key' => 'product_id', ], ]; } }
- Create parent via HTTP request with staging IDs in the request:
// In your controller's store method public function store(Request $request) { // Stage children first (if coming from form) $stockLevelIds = StockLevel::stageMany([ ['quantity' => 100, 'location' => 'Warehouse A'], ['quantity' => 50, 'location' => 'Warehouse B'], ]); // Stage parent $product = new Product($request->only(['name', 'price'])); $productStagingId = $product->stage(); // Link children StockLevel::linkStagedToParent($stockLevelIds, $productStagingId, Product::class); // Create product - staging IDs must be in the request for automatic saving $product = Product::create($request->only(['name', 'price']) + [ '_staging_id' => $productStagingId, 'stock_levels_staging_ids' => $stockLevelIds, ]); // Children are automatically saved with product_id set! return $product; }
Note: The automatic child saving works by checking the request for _staging_id and {relationship_name}_staging_ids keys. These keys must be present in the request when creating the parent model.
Get Staged Collection for Display
Get a collection of staged objects for display in forms or tables:
$stagingIds = StockLevel::stageMany([ ['quantity' => 100, 'location' => 'Warehouse A'], ['quantity' => 50, 'location' => 'Warehouse B'], ]); $collection = StockLevel::getStagedCollection($stagingIds); // Use like Eloquent models foreach ($collection as $item) { echo $item->location; // 'Warehouse A', 'Warehouse B' echo $item->is_staged; // true }
Staging Endpoints (Optional)
You can use the included Route macro to easily add staging endpoints to your controllers:
// In routes/web.php or routes/api.php use Illuminate\Support\Facades\Route; Route::resourceWithStage('products', ProductController::class);
This will create:
POST /products/stage- Stage a product (in addition to standard resource routes)- All standard resource routes (
GET /products,POST /products, etc.)
Your controller should implement a stage method:
use Illuminate\Http\Request; class ProductController extends Controller { public function stage(Request $request) { $validatedData = $request->validate([ 'name' => 'required|string', 'price' => 'required|numeric', ]); $product = new Product($validatedData); $stagingId = $product->stage(); return response()->json([ 'message' => 'Product staged successfully.', 'staging_id' => $stagingId, ]); } }
Note: Your Product model must use the Stagable trait for the stage() method to be available.
Storing References to Staged Models
You can store references to staged models in your attributes (useful for belongsTo relationships):
// Stage supplier first $supplierStagingId = Supplier::stageNew(['name' => 'Acme Corp']); // Stage product with reference to staged supplier $productStagingId = Product::stageNew([ 'name' => 'Widget', 'supplier_staging_id' => $supplierStagingId, // Store reference ]); // Later, when promoting, you'll need to handle the relationship manually: $stagedProduct = Product::findStaged($productStagingId); $stagedSupplier = Supplier::findStaged($stagedProduct->supplier_staging_id); $realSupplier = $stagedSupplier->promoteFromStaging(); $stagedProduct->supplier_id = $realSupplier->id; // Set actual foreign key $realProduct = $stagedProduct->promoteFromStaging();
Features
✅ Stage any Eloquent model as a draft
✅ Stage multiple children at once with stageMany()
✅ Link staged children to staged parents
✅ Automatic saving of hasMany children when parent is created
✅ Preserve sort order for staged items
✅ Get staged collections for display (model-like objects)
✅ Clean promotion to production tables
✅ No intrusive columns in your main tables
Enjoy easy staging without cluttering your main table with extra columns!
Testing
composer test
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
Credits
License
The MIT License (MIT). Please see License File for more information.
albaroody/staging 适用场景与选型建议
albaroody/staging 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 76 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 04 月 29 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「laravel」 「staging」 「albaroody」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 albaroody/staging 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 albaroody/staging 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 albaroody/staging 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Quickly add some password protection to a staging site.
Versioned, environment-based Seeders in Laravel. Used while original does not update to Laravel 5.7
Laravel mail transport for Sinkhole — a Cloudflare Worker email trap
Adds basic auth to your Silverstripe website the way it was intended (not just for pages, also for files).
staging
Alfabank REST API integration
统计信息
- 总下载量: 76
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 15
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-04-29