Download the PHP package daltonmccleery/remote-models without Composer
On this page you can find all versions of the php package daltonmccleery/remote-models. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download daltonmccleery/remote-models
More information about daltonmccleery/remote-models
Files in daltonmccleery/remote-models
Package remote-models
Short Description Sometimes you want to use Eloquent, but that data is in another database on a different application.
License MIT
Informations about the package remote-models
Remote Models
Sometimes you want to use Eloquent, but that data is in another database on a different application.
This package is used for both the "host" application and the "remote" application. Both use cases are detailed below, or you can look at these examples.
Requirements
The pdo-sqlite
PHP extension must be installed on your system to use this package.
You will need to set up any endpoints on the host application where the data is stored.
Install
Publishing the Config
It is recommended to publish the config file; this will allow you to add a host domain, API pathing, and any Models.
Remote Use
Using this package consists of two steps:
- Add the
RemoteModel
trait to a model.
That's it.
Now, you can use this model anywhere you like, and it will behave as if the table exists in your application.
This will allow you to add a host domain, API pathing, and any Models.
Custom Endpoint
You may provide a custom endpoint to your Remote Model that will be called when the model is loaded. If you do this and have this package installed on your host application, you will need to create your own API endpoint.
Custom Schema
Remote Models will auto-discover the schema from the first API call, however, if you want more control over the schema or
what fields are saved locally, you may add them to a $schema
property with a type cast for the column.
Host Use
This package is dependent on a separate Laravel application that "hosts" the Models and their data. You will need to either install this package on the host application as well and enter the "remote" models you wish to expose to the "remote" application.
Custom Endpoint
If you use a custom endpoint on the remote application, you will need to set up that custom route. You can use the following as an example:
If you do not install this package on the "host" application, a custom endpoint will need to be set up for each Remote Model you plan on using. You will also need to manually validate the given API Key. You can use the following as an example:
You are not required to install this package on the "host" application. If you don't, you will need to set up your own API endpoints for the models you wish to use. It is recommended to use Laravel's default pagination.
Cache Interval
You can set how long to cache the remote data for using the cache-ttl
config option. These values follow the standard
[DateTime Interval](https://www.php.net/manual/en/class.dateinterval.php properties. Below are a few examples:
External Host Data
There may be instances where you do not control the "host" application, i.e. it could be a Google Spreadsheet or a 3rd party API, but would still like a way to query that data using Eloquent.
You'll need to have your Model implement the RemoteModelInterface
and include the base RemoteModelManagement
trait.
This will have you implement 2 methods for setting up a custom schema (optional) and recursively fetching the data. In this instance,
the $remoteEndpoint
is optional.
You can create your own Remote Model by following this example:
How It Works
When a Model is called, it will make an API call to the either a custom endpoint or to a predefined endpoint using the Model's name. This predefined API endpoint can be configured in the config file.
Under the hood, this package creates and caches a SQLite database just for this model and all its data. It creates a table and populates it with the returned, paginated API data. If, for whatever reason, it can't cache a .sqlite file, it will default to using an in-memory sqlite database.
This package was heavily inspired by Caleb Porzio's Sushi package.
Upcoming Features
- Add command for pre-caching all Remote Models for deployment.
- Add local database fallback
- Add support for other data sources (Spreadsheets, external APIs, etc)
All versions of remote-models with dependencies
ext-pdo_sqlite Version *
ext-sqlite3 Version *
guzzlehttp/guzzle Version ^7.8
illuminate/database Version ^10.0 || ^11.0
illuminate/support Version ^10.0 || ^11.0