PHP code example of albaroody / staging

1. Go to this page and download the library: Download albaroody/staging library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

albaroody / staging example snippets


use Albaroody\\Staging\\Traits\\Stagable;

class Patient extends Model
{
    use Stagable;
}

// 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',
]);

$stagedPatient = Patient::findStaged($stagingId);

// Now you can use it like a normal model
echo $stagedPatient->name;

$realPatient = $stagedPatient->promoteFromStaging();

// The staging entry is automatically deleted after promotion

$stockLevels = [
    ['quantity' => 100, 'location' => 'Warehouse A', '_sort_order' => 0],
    ['quantity' => 50, 'location' => 'Warehouse B', '_sort_order' => 1],
];

$stagingIds = StockLevel::stageMany($stockLevels);

// 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);

$stagedChildren = StockLevel::findStagedChildren($productStagingId, Product::class);

foreach ($stagedChildren as $child) {
    echo $child['data']['location']; // 'Warehouse A', 'Warehouse B'
}

// 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
);

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',
            ],
        ];
    }
}

// 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;
}

$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
}

// In routes/web.php or routes/api.php
use Illuminate\Support\Facades\Route;

Route::resourceWithStage('products', ProductController::class);

use Illuminate\Http\Request;

class ProductController extends Controller
{
    public function stage(Request $request)
    {
        $validatedData = $request->validate([
            'name' => 'essage' => 'Product staged successfully.',
            'staging_id' => $stagingId,
        ]);
    }
}

// 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();
bash
php artisan vendor:publish --tag="staging-migrations"
php artisan migrate
bash
php artisan vendor:publish --tag="staging-config"