PHP code example of grizzlyware / model-swapper

1. Go to this page and download the library: Download grizzlyware/model-swapper 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/ */

    

grizzlyware / model-swapper example snippets


// Code which may exist out of your control, in a vendor's package for example
\Vendor\Package\Models\CountryCode::firstOrFail()
// \Vendor\Package\Models\CountryCode is returned

/**
* After using Model Swapper
*/

\Vendor\Package\Models\CountryCode::firstOrFail()
// \App\Models\CountryCode is returned

use Grizzlyware\ModelSwapper\Facades\ModelSwapper;
use Grizzlyware\ModelSwapper\Traits\IsReplacementModel;

// Add the IsReplacementModel trait to your replacement class
class YourApplicationsClass extends ClassFromVendorPackage
{
    use IsReplacementModel;
}

// Swap the model out - this would usually be in your AppServiceProvider, in the boot method.
ModelSwapper::swap(
    ClassFromVendorPackage::class,
    YourApplicationsClass::class
);

// From now on, all queries for ClassFromVendorPackage will return instances of YourApplicationsClass

use Grizzlyware\ModelSwapper\Contracts\ModelSwapperServiceInterface;

// Or typehint from the container
public function __construct(ModelSwapperServiceInterface::class $modelSwapper)

// Or resolve from the container
resolve(ModelSwapperServiceInterface::class)->swap(
    ClassFromVendorPackage::class,
    YourApplicationsClass::class
);

// You can access the original, non-replaced model query like so:
YourApplicationsClass::replacedModelQuery()->firstOrFail();