PHP code example of daltonmccleery / remote-models

1. Go to this page and download the library: Download daltonmccleery/remote-models 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/ */

    

daltonmccleery / remote-models example snippets


class Celebrity extends Model
{
    use \RemoteModels\RemoteModel;
}

$celebrity = Celebrity::where('name', 'Dwayne Johnson')->first();

class Celebrity extends Model
{
    use \RemoteModels\RemoteModel;

    protected $remoteEndpoint = '/v1/celebrities';
}

class Celebrity extends Model
{
    use \RemoteModels\RemoteModel;

    protected $remoteSchema = [
        'name' => 'string',
        'birthday' => 'datetime' 
    ];
}

// config/remote-models.php
'host-models' => [
    \App\Models\Celebrity::class
]

Route::post(
    config('remote-models.api-path') . '/v1/celebrities',
    fn (\RemoteModels\Http\Requests\CustomRemoteModelRequest $request) => $request->returnRemoteModels(Celebrity::class)
);

Route::post('/api/_remote/_models/v1/celebrities', fn () => response()->json(Celebrity::paginate())));

// config/remote-models.php
'cache-ttl' => '1m | 1w | 1d | 1h' // 1 month | 1 week | 1 day | 1 hour

class Celebrity extends Model implements \RemoteModels\Interfaces\RemoteModelInterface
{
    use \RemoteModels\RemoteModelManagement;

    public function migrate(): void
    {
        $this->createRemoteModelTable(schemaCallback: function (array $schema) {
        
            // Make any modifications to the column schema before the sqlite table is created.

            return $schema;
        });

        $this->loadRemoteModelData();
    }
    
    public function loadRemoteModelData(int $page = 1): void
    {
        // Normal operation is a POST request with the config API key,
        // but you are free to modify the API call as you like.
        $response = \Illuminate\Support\Facades\Http::get($this->getRemoteModelEndpoint());
        
        $data = $response->json();

        // `insertRemoteModelData` is available and takes an array of data to be inserted.
        $this->insertRemoteModelData($data['data'], $data['per_page'] ?? 15);

        // Call the next page, if available.
        if ((int) $data['current_page'] < (int) $data['last_page']) {
            $this->loadRemoteModelData((int) $data['current_page'] + 1);
        }
    }
}
console
php artisan vendor:publish --provider="RemoteModels\RemoteModelsServiceProvider" --force